| | |
number game
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 0
#Guess my number
#The computer picks a random number between 1 and 100
#the player tries to guess it an dhte computer lets
#the player know if the guess is too hight, too low
#or right on the money
import random
print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible. \n"
#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
#guessing loop
while (guess != the_number):
if (guess > the_number)
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
print "You guessed it! the number was", the_number
print "And it only took you", tries, "tries!\n"
#this is the end
#this is a copy of the script run on python
>>> #Guess my number
...
>>> #The computer picks a random number between 1 and 100
... #the player tries to guess it an dhte computer lets
... #the player know if the guess is too hight, too low
... #or right on the money
...
>>> import random
>>>
>>> print "\tWelcome to 'Guess My Number'!"
Welcome to 'Guess My Number'!
>>> print "\nI'm thinking of a number between 1 and 100."
I'm thinking of a number between 1 and 100.
>>> print "Try to guess it in as few attempts as possible. \n"
Try to guess it in as few attempts as possible.
>>>
>>> #set the initial values
...
>>> the_number = random.randrange(100) + 1
>>> guess = int(raw_input("Take a guess: "))
Take a guess: tries = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'tries = 1'
>>>
>>> #guessing loop
...
>>> while (guess != the_number):
... if (guess > the_number)
File "<stdin>", line 2
if (guess > the_number)
^
SyntaxError: invalid syntax
>>> print "Lower..."
File "<stdin>", line 1
print "Lower..."
^
IndentationError: unexpected indent
>>> else:
File "<stdin>", line 1
else:
^
IndentationError: unexpected indent
>>> print "Higher..."
File "<stdin>", line 1
print "Higher..."
^
IndentationError: unexpected indent
>>>
>>> guess = int(raw_input("Take a guess: "))
Take a guess: tries += 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'tries += 1'
>>>
>>> print "You guessed it! the number was", the_number
You guessed it! the number was 18
>>> print "And it only took you", tries, "tries!\n"
And it only took you
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tries' is not defined
Running Ubuntu Gutsy gibbons, and emacs
I have checked the code carefully but can't get past the first traceback error.
Thanks for any help.
#The computer picks a random number between 1 and 100
#the player tries to guess it an dhte computer lets
#the player know if the guess is too hight, too low
#or right on the money
import random
print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible. \n"
#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
#guessing loop
while (guess != the_number):
if (guess > the_number)
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
print "You guessed it! the number was", the_number
print "And it only took you", tries, "tries!\n"
#this is the end
#this is a copy of the script run on python
>>> #Guess my number
...
>>> #The computer picks a random number between 1 and 100
... #the player tries to guess it an dhte computer lets
... #the player know if the guess is too hight, too low
... #or right on the money
...
>>> import random
>>>
>>> print "\tWelcome to 'Guess My Number'!"
Welcome to 'Guess My Number'!
>>> print "\nI'm thinking of a number between 1 and 100."
I'm thinking of a number between 1 and 100.
>>> print "Try to guess it in as few attempts as possible. \n"
Try to guess it in as few attempts as possible.
>>>
>>> #set the initial values
...
>>> the_number = random.randrange(100) + 1
>>> guess = int(raw_input("Take a guess: "))
Take a guess: tries = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'tries = 1'
>>>
>>> #guessing loop
...
>>> while (guess != the_number):
... if (guess > the_number)
File "<stdin>", line 2
if (guess > the_number)
^
SyntaxError: invalid syntax
>>> print "Lower..."
File "<stdin>", line 1
print "Lower..."
^
IndentationError: unexpected indent
>>> else:
File "<stdin>", line 1
else:
^
IndentationError: unexpected indent
>>> print "Higher..."
File "<stdin>", line 1
print "Higher..."
^
IndentationError: unexpected indent
>>>
>>> guess = int(raw_input("Take a guess: "))
Take a guess: tries += 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'tries += 1'
>>>
>>> print "You guessed it! the number was", the_number
You guessed it! the number was 18
>>> print "And it only took you", tries, "tries!\n"
And it only took you
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tries' is not defined
Running Ubuntu Gutsy gibbons, and emacs
I have checked the code carefully but can't get past the first traceback error.
Thanks for any help.
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
Solved Threads: 0
This is the code -
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"
#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
raw_input ("\n\nPress the enter key to exit.")
Now the problem is how do you limit the amount of guesses.
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"
#set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
raw_input ("\n\nPress the enter key to exit.")
Now the problem is how do you limit the amount of guesses.
Use code tags:
[code=python]
#Code in here
[/code]
This will make your code readable for us forum members, which in turn helps us to solve your problem.
In order to limit the number of guesses, you should be using an
[code=python]
#Code in here
[/code]
This will make your code readable for us forum members, which in turn helps us to solve your problem.
In order to limit the number of guesses, you should be using an
if guesses == allowed_guesses: statement, or something similar. •
•
Join Date: Nov 2008
Posts: 56
Reputation:
Solved Threads: 0
I set the amount of tries to less than 3 ,however when I make more than 3 guesses it won't up the message. What did I do wrong?
Editor's note:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.
[code=python]
your Python code here
[/code]
python Syntax (Toggle Plain Text)
# Guess My Number # The computer picks a random number import random print "\tWelcome to 'Guess my number'!:" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" #set the initial values the_number = random.randrange(100) + 1 guess = int(raw_input("Take a guess: ")) tries = 1 # guessing loop while (guess != the_number): if (guess > the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess: ")) if tries > 3: print "You have more than 3 attempts, game over." tries += 1 else: print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input ("\n\nPress the enter key to exit.")
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.
[code=python]
your Python code here
[/code]
Last edited by vegaseat; Nov 17th, 2008 at 6:32 pm. Reason: added code tags
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
•
•
•
•
I set the amount of tries to less than 3 ,however when I make more than 3 guesses it won't up the message. What did I do wrong?
Python Syntax (Toggle Plain Text)
# Guess My Number # The computer picks a random number import random print "\tWelcome to 'Guess my number'!:" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" #set the initial values the_number = random.randrange(100) + 1 guess = int(raw_input("Take a guess: ")) tries = 1 # guessing loop while (guess != the_number): if (guess > the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take a guess: ")) if tries > 3: print "You have more than 3 attempts, game over." tries += 1 else: print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" raw_input ("\n\nPress the enter key to exit.")
Change the loop to while True. Omit the first guess. Initialize tries to 0, and increment tries after guess. Modify your if/else to if/elif/else. The else is for a correct guess --> break. If the user made 3 guesses --> break.
Python Syntax (Toggle Plain Text)
import random print "\tWelcome to 'Guess my number'!:" print "\nI'm thinking of a number between 1 and 100." print "Try to guess it in as few attempts as possible.\n" the_number = random.randrange(1,101) tries = 0 while True: guess = int(raw_input("Take a guess: ")) tries += 1 if guess > the_number: print "Lower..." elif guess < the_number: print "Higher..." else: print "You guessed it! The number was", the_number print "And it only took you", tries, "tries!\n" break if tries == 3: print "You have made 3 attempts, game over." break raw_input ("\n\nPress the enter key to exit.")
Last edited by solsteel; Nov 17th, 2008 at 6:33 pm.
•
•
Join Date: Nov 2008
Posts: 56
Reputation:
Solved Threads: 0
•
•
•
•
Are you referring to the code I posted?. Variable tries is equal to 3 after 3 guesses. Are you going to allow 4 guesses?
When I set it to tries > 3 then it would allow 3 guesses.
Are you getting a different result?
Last edited by dseto200; Nov 18th, 2008 at 6:17 pm. Reason: modifying comment
![]() |
Similar Threads
- How do i create a simple game using c++?? (C++)
- Number Game- Reset, instructions, ehancements! (VB.NET)
- Guess Number Game (Problem) (Java)
- The Number Game (Software Development Job Offers)
- Help with random number gen (C++)
Other Threads in the Python Forum
- Previous Thread: Which gui toolkit and why??
- Next Thread: Absurd Loop Problem
| Thread Tools | Search this Thread |
abrupt accessdenied advanced ansi anti apache application approximation argv array backend beginner binary builtin calculator change command converter countpasswordentry csv curved dan08 def dictionary edit event file float format function google heads homework inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote return reverse scrolledtext session software sprite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable voip wordgame write wxpython






