number game

Reply

Join Date: Mar 2008
Posts: 1
Reputation: VinceW is an unknown quantity at this point 
Solved Threads: 0
VinceW VinceW is offline Offline
Newbie Poster

number game

 
0
  #1
Mar 22nd, 2008
#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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 917
Reputation: linux is an unknown quantity at this point 
Solved Threads: 26
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: number game

 
0
  #2
Mar 22nd, 2008
Use code tags, please.

And try declaring tries as an integer at the start of your application.
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: number game

 
0
  #3
Nov 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,001
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 244
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: number game

 
0
  #4
Nov 17th, 2008
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 if guesses == allowed_guesses: statement, or something similar.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: number game

 
0
  #5
Nov 17th, 2008
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?
  1. # Guess My Number
  2. # The computer picks a random number
  3.  
  4. import random
  5. print "\tWelcome to 'Guess my number'!:"
  6. print "\nI'm thinking of a number between 1 and 100."
  7. print "Try to guess it in as few attempts as possible.\n"
  8.  
  9. #set the initial values
  10. the_number = random.randrange(100) + 1
  11. guess = int(raw_input("Take a guess: "))
  12. tries = 1
  13.  
  14. # guessing loop
  15.  
  16. while (guess != the_number):
  17. if (guess > the_number):
  18. print "Lower..."
  19. else:
  20. print "Higher..."
  21.  
  22. guess = int(raw_input("Take a guess: "))
  23. if tries > 3:
  24. print "You have more than 3 attempts, game over."
  25. tries += 1
  26. else:
  27. print "You guessed it! The number was", the_number
  28. print "And it only took you", tries, "tries!\n"
  29.  
  30. raw_input ("\n\nPress the enter key to exit.")
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]
Last edited by vegaseat; Nov 17th, 2008 at 6:32 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: number game

 
0
  #6
Nov 17th, 2008
Originally Posted by dseto200 View Post
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?

  1. # Guess My Number
  2. # The computer picks a random number
  3.  
  4. import random
  5. print "\tWelcome to 'Guess my number'!:"
  6. print "\nI'm thinking of a number between 1 and 100."
  7. print "Try to guess it in as few attempts as possible.\n"
  8.  
  9. #set the initial values
  10. the_number = random.randrange(100) + 1
  11. guess = int(raw_input("Take a guess: "))
  12. tries = 1
  13.  
  14. # guessing loop
  15.  
  16. while (guess != the_number):
  17. if (guess > the_number):
  18. print "Lower..."
  19. else:
  20. print "Higher..."
  21.  
  22. guess = int(raw_input("Take a guess: "))
  23. if tries > 3:
  24. print "You have more than 3 attempts, game over."
  25. tries += 1
  26. else:
  27. print "You guessed it! The number was", the_number
  28. print "And it only took you", tries, "tries!\n"
  29.  
  30. raw_input ("\n\nPress the enter key to exit.")
I cannot read your code without code tags!

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.
  1. import random
  2. print "\tWelcome to 'Guess my number'!:"
  3. print "\nI'm thinking of a number between 1 and 100."
  4. print "Try to guess it in as few attempts as possible.\n"
  5.  
  6. the_number = random.randrange(1,101)
  7.  
  8. tries = 0
  9.  
  10. while True:
  11. guess = int(raw_input("Take a guess: "))
  12. tries += 1
  13. if guess > the_number:
  14. print "Lower..."
  15. elif guess < the_number:
  16. print "Higher..."
  17. else:
  18. print "You guessed it! The number was", the_number
  19. print "And it only took you", tries, "tries!\n"
  20. break
  21. if tries == 3:
  22. print "You have made 3 attempts, game over."
  23. break
  24.  
  25. raw_input ("\n\nPress the enter key to exit.")
Last edited by solsteel; Nov 17th, 2008 at 6:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: number game

 
0
  #7
Nov 17th, 2008
Oops! Accidental duplicate post.
Last edited by solsteel; Nov 17th, 2008 at 6:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: number game

 
0
  #8
Nov 17th, 2008
Thanks only had to make 1 correction.
Had to set
If tries > 3:
Because
If tries = = 3: it would only allow up to 2 guesses.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: number game

 
0
  #9
Nov 17th, 2008
Are you referring to the code I posted?. Variable tries is equal to 3 after 3 guesses. Are you going to allow 4 guesses?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 56
Reputation: dseto200 is an unknown quantity at this point 
Solved Threads: 0
dseto200 dseto200 is offline Offline
Junior Poster in Training

Re: number game

 
0
  #10
Nov 18th, 2008
Originally Posted by solsteel View Post
Are you referring to the code I posted?. Variable tries is equal to 3 after 3 guesses. Are you going to allow 4 guesses?
Hi Solsteel, Yes i'm referring to the code you posted, because when I inserted the code - if tries == 3 it would only allow 2 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC