470,594 Members | 1,499 Online

# help with this game

ok now that i know the random function, i made this guessing game. I get an
error though, and Im new so im not to good at figuring out what its talking
import random
a = random.randint(1, 100)
b=-100
c=0
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while a != b:
c = c + 1
b = input ("enter guess:")
if b < a :
print "to low"
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
elif b > a :
print ("to high")
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
print "you got it in ",c," tries"
Jul 18 '05 #1
6 1538 # You can do it like this:

import random
import sys
from time import sleep

class Mygame:

def __init__(self):
self.a = random.randint(1, 100)
self.c = 0
self.MAX_TRIES = 10

def startit(self):
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while True:
while self.c == self.MAX_TRIES:
print "You have no more tries sorry."
sleep(5)
sys.exit()

self.b = input("Guess a number: ")

if self.b == self.a:
print "you got it in ",self.c," tries"
sleep(5)
sys.exit()

while self.b != self.a:
self.c = self.c + 1
if self.b < self.a :
print "to low"
break
elif self.b > self.a :
print ("to high")
break
game = Mygame()
game.startit()

----- Original Message -----
From: "Alex Endl" <al******@hotmail.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Saturday, July 17, 2004 6:08 PM
Subject: help with this game

ok now that i know the random function, i made this guessing game. I get an
error though, and Im new so im not to good at figuring out what its talking

import random
a = random.randint(1, 100)
b=-100
c=0
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while a != b:
c = c + 1
b = input ("enter guess:")
if b < a :
print "to low"
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
elif b > a :
print ("to high")
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
print "you got it in ",c," tries"

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #2
Hi.

Here is the error your code produces on my machine:

Traceback (most recent call last):
File
"C:\Python23\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py" ,
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\My Documents\Sean\hacks\guessthenumber.py", line 15, in ?
print "trie number", c ("out of 10")
TypeError: 'int' object is not callable
The last line of the error message is saying that you are trying to make a
method or function call using an int, and that an int is not callable

c ("out of 10") <-- this is function/method call notation

The problem is you've misplaced your quotation marks in

print "trie number", c ("out of 10")

it should be

print "trie number", c, "(out of 10)"

Or, if you want to correct the spelling:

print "try number", c, "(out of 10)"

Changing the two occurrences of that line in your code, will solve that
problem. There are other problems with your code, though.

You only want to give players 10 chances to guess the number - but, the code
you have at the moment won't accomplish that. If the player keeps guessing
incorrectly, then, on the tenth turn, they'll see

to many guesses <-- should be "too many
guesses"
try number 10 (out of 10)

and then they'll be allowed to continue guessing forever, or until they
guess the number. Can you see why? The solution is straightforward, so I'll
leave that alone so that you can learn how to solve the problem.

There are two other things I'd like to point out for you:

1. You say the following twice in the same loop (see if you can figure out
how to say it only once):

if c == 10:
print "to many guesses"
print "trie number", c ("out of 10") <-- remember to fix this
bug first though

2. Try changing 'a', 'b', and 'c' to something more meaningful, say

- 'number', or 'theNumber', or 'theNumberToGuess' instead of 'a'
- 'guess', or 'theGuess', or 'playersGuess', or 'theNumberGuessed' instead
of 'b'
- 'tries', or 'numberOfTries' instead of 'c'
Oh, and

a = random.randint(1, 100)

should be

a = random.randint(1, 101)

if you want people to guess a number between 1 and 100, inclusive.

Hope that helps,
Sean
"Alex Endl" <al******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
ok now that i know the random function, i made this guessing game. I get an error though, and Im new so im not to good at figuring out what its talking about.
import random
a = random.randint(1, 100)
b=-100
c=0
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while a != b:
c = c + 1
b = input ("enter guess:")
if b < a :
print "to low"
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
elif b > a :
print ("to high")
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
print "you got it in ",c," tries"

Jul 18 '05 #3
Hello,

"Alex Endl" <al******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
ok now that i know the random function, i made this guessing game. I get an error though, and Im new so im not to good at figuring out what its talking about.
import random
a = random.randint(1, 100)
b=-100
c=0
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while a != b:
If you want it to stop after ten tries, this should read:
while a != b and c < 10:
c = c + 1
b = input ("enter guess:")
if b < a :
print "to low"
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
I get the error "int object is not callable". This is because the line
print "try number", c, "out of 10"

With the brackets in there Python thinks you are trying to call a function
c("out of 10"). But c isn't a function, so it complains.
elif b > a :
print ("to high")
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
Same here:
print "try number", c, "out of 10"
print "you got it in ",c," tries"
if a == b:
print "you got it in", c, "tries"

(so it'll only say that if you did get it)

Nick
Jul 18 '05 #4
Alex Endl wrote:
ok now that i know the random function, i made this guessing game. I get an
error though, and Im new so im not to good at figuring out what its talking
When you need help figuring out an error, be sure to post your exception
traceback.
This is the one I got when I ran your code:

Traceback (most recent call last):
File "...\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "...\greg\My Documents\Projects\Script1.py", line 15, in ?
print "trie number", c ("out of 10")
TypeError: 'int' object is not callable

import random
a = random.randint(1, 100)
b=-100
c=0
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while a != b:
c = c + 1
b = input ("enter guess:")
if b < a :
print "to low"
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
The problem is on this line (and line 20 also, of course). What you most
likely want is:
print "trie number", c, "(out of 10)"
Note that I added a comma and moved the quotation marks. The syntax
foo(bar) is for calling function named foo with an argument called bar.
Just like randint and input. So Python thought c was your function and
"out of 10" was an argument. But, alas, c in an int, not a function, so
it couldn't call it. That's the error.
elif b > a :
print ("to high")
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
print "you got it in ",c," tries"

No problem, my time is worthless. ;)

greg

Jul 18 '05 #5
thanks, helped a lot
"Alex Endl" <al******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
ok now that i know the random function, i made this guessing game. I get an error though, and Im new so im not to good at figuring out what its talking about.
import random
a = random.randint(1, 100)
b=-100
c=0
print "Welcome to guess the number"
print "to play type in a number between 1 and 100."
print "but you only get ten tries"
while a != b:
c = c + 1
b = input ("enter guess:")
if b < a :
print "to low"
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
elif b > a :
print ("to high")
if c == 10:
print "to many guesses"
print "trie number", c ("out of 10")
print "you got it in ",c," tries"

Jul 18 '05 #6
"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message news:<0d*********************@news20.bellglobal.co m>...
....
Oh, and

a = random.randint(1, 100)

should be

a = random.randint(1, 101)

if you want people to guess a number between 1 and 100, inclusive.

random.randint(1, 100) _can_ return 100. It's randrange that doesn't.
Jul 18 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 3 posts views Thread by Roman | last post: by 1 post views Thread by rainbowii7 | last post: by reply views Thread by dnphamus13 | last post: by 7 posts views Thread by Gasten | last post: by 1 post views Thread by hpbutterbeer | last post: by 2 posts views Thread by The alMIGHTY N | last post: by 2 posts views Thread by Jingx | last post: by 1 post views Thread by Gomi | last post: by 2 posts views Thread by LilMeechc20 | last post: by reply views Thread by Trystan | last post: by 2 posts views Thread by davidbuttler | last post: by 2 posts views Thread by AlexandraMT | last post: by reply views Thread by lanliddd | last post: by reply views Thread by ryjfgjl | last post: by reply views Thread by AlexandraMT | last post: by reply views Thread by drunkenHiker | last post: by 1 post views Thread by markleo | last post: by 2 posts views Thread by Eliasendashaw | last post: by