473,466 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
6 1650
# 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
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
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"
thanks for your time

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
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
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
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"
thanks for your time

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roman | last post by:
I've been trying this one for 2-3 hours and can't figure it out. I'de appreciate any help or pointers in the right direction. Thanks. Query I need the query to return me all the lottery names...
1
by: rainbowii7 | last post by:
Calling all programmers for helllllllllllllllppppp!!! i am currently doing a uni degree and our lecturers have set us the task of making a game in JavaScript. i chose to do a hangman game and...
0
by: dnphamus13 | last post by:
I'm new to this and drowning right now. I would like to put my database online for viewing. I managed to do the filtering but i need to do PAGING as the XML doc get bigger. From what i understand...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
2
by: The alMIGHTY N | last post by:
I've been having some issues with recursive summing and have been unsuccessful at getting some of the solutions I've seen online to work. I'm hoping that perhaps somebody in the newsgroup will be...
2
by: Jingx | last post by:
Please help us to figure out the name chart for the following game. 1. 20 Females and 20 Males are invited to the game, in this game; there are total 10 runs for 10 games. 2. 1 F and 1 M are...
1
by: Gomi | last post by:
Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.