435,538 Members | 2,225 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,538 IT Pros & Developers. It's quick & easy.

help with this game

 P: n/a 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" thanks for your time Jul 18 '05 #1
6 Replies

 P: n/a # 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" Newsgroups: comp.lang.python To: 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 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" thanks for your time -- http://mail.python.org/mailman/listinfo/python-list Jul 18 '05 #2

 P: n/a 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" 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" thanks for your time Jul 18 '05 #3

 P: n/a Hello, "Alex Endl" 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 should read: 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) thanks for your time Nick Jul 18 '05 #4

 P: n/a 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 about. 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 Now, on to your code: 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" thanks for your time No problem, my time is worthless. ;) greg Jul 18 '05 #5

 P: n/a thanks, helped a lot "Alex Endl" 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" thanks for your time Jul 18 '05 #6

 P: n/a "Sean Ross" 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. 