number game

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

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
  #11
Nov 18th, 2008
Think about it. tries starts out as 0. Make a guess. tries is incremented to 1.
guess - 2
guess - 3

That's 3 guesses! How could it only be 2? Remember, I omitted the guess outside of the loop in the code I posted. It must still be in the code you tested.
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
  #12
Nov 18th, 2008
I found the mistake my tries was set to tries = 1 it should be tries = 0.
Thanks for all your help solsteel much appreciated.
Last edited by dseto200; Nov 18th, 2008 at 7:42 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
  #13
Nov 18th, 2008
You are very welcome!
Last edited by solsteel; Nov 18th, 2008 at 8:17 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
  #14
Nov 18th, 2008
Confirmed it works.

Now I have to figure how to modify the program when player picks a random number and the computer will have to guess my random number.
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
  #15
Nov 18th, 2008
Here's a hint:
  1. guess = random.choice(possible_guesses)
Update possible_guesses every time a guess is made, eliminating the numbers that are greater than or less than the last guess.
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
  #16
Nov 19th, 2008
Is this the only way to solve this problem. Looking at the chapter i'm reading "Python Programming for the absolute Beginner" they don't even list that command. If it is I have to complain to the author on why he didn't even mention this command.
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
  #17
Nov 19th, 2008
Here's the program for computer guessing my number unfortunately when I run the program
it's off by 1. For example I enter my number 24 computer guessed it correctly on the 3rd attempt.
Unfortunately it list "And it only took two tries".
I set tries=1.
If I set tries=0 it will always list it as "And it only took 1 tries".


import random
print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 100."
print "I will try to guess it in as few attempts as possible.\n"

number = input ("Enter the number: ")

#the computer guesses the number using the random function
guess = random.randrange (1,101)
tries = 1

#FIXME
while (guess != number):
if (guess > number):
print "You chose", guess, "the number is Lower ..."
guess = random.randint(1, guess-1)

else:
print "You chose", guess, "the number is Higher ..."
guess = random.randint(guess+1, 101)
tries += 1

print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"
break
raw_input ("Press <ENTER> to exit.")
Last edited by dseto200; Nov 19th, 2008 at 5:09 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
  #18
Nov 19th, 2008
Resolved it had to change the location of tries=1

import random
print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 100."
print "I will try to guess it in as few attempts as possible.\n"

number = input ("Enter the number: ")

#the computer guesses the number using the random function
guess = random.randrange (1,501)
tries = 1

#FIXME
while (guess != number):
tries += 1
if (guess > number):
print "You chose", guess, "the number is Lower ..."
guess = random.randint(1, guess+1)

else:
print "You chose", guess, "the number is Higher ..."
guess = random.randint(guess-1, 501)


print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"
break
raw_input ("Press <ENTER> to exit.")
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
  #19
Nov 20th, 2008
What is the hang-up you have with CODE TAGS? Your code would look like below, and other people may actually read it!

[code=Python] ......code goes here....
....and here....
[/code]



Originally Posted by dseto200 View Post
Resolved it had to change the location of tries=1
  1. import random
  2. print "\t\t\tWelcome to \"Guess My Number\"!"
  3. print "\nThink of a number between 1 and 100."
  4. print "I will try to guess it in as few attempts as possible.\n"
  5.  
  6. number = input ("Enter the number: ")
  7.  
  8. #the computer guesses the number using the random function
  9. guess = random.randrange (1,501)
  10. tries = 1
  11.  
  12. #FIXME
  13. while (guess != number):
  14. tries += 1
  15. if (guess > number):
  16. print "You chose", guess, "the number is Lower ..."
  17. guess = random.randint(1, guess+1)
  18.  
  19. else:
  20. print "You chose", guess, "the number is Higher ..."
  21. guess = random.randint(guess-1, 501)
  22.  
  23.  
  24. print "You guessed it! The number was", number
  25. print "And it only took you", tries, "tries!\n"
  26. break
  27. raw_input ("Press <ENTER> to exit.")
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
  #20
Nov 20th, 2008
Testing with code tags
  1. import random
  2. print "\t\t\tWelcome to \"Guess My Number\"!"
  3. print "\nThink of a number between 1 and 500."
  4. print "I will try to guess it in as few attempts as possible.\n"
  5.  
  6. number = input ("Enter the number: ")
  7.  
  8. #the computer guesses the number using the random function
  9. guess = random.randrange (1,501)
  10. tries = 1
  11.  
  12. #FIXME
  13. while True:
  14. guess != number
  15. tries += 1
  16. if (guess > number):
  17. print "You chose", guess, "the number is Lower ..."
  18. guess = random.randint(1, guess+1)
  19.  
  20. else:
  21. print "You chose", guess, "the number is Higher ..."
  22. guess = random.randint(guess-1, 501)
  23.  
  24.  
  25. print "You guessed it! The number was", number
  26. print "And it only took you", tries, "tries!\n"
  27. break
  28. raw_input ("\n\nPress the enter key to exit.")
  29.  
  30. if tries == 20:
  31. print "You have more than 20 guesses, game over."
  32. break
  33. raw_input ("\n\nPress the enter key to exit.")
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


Views: 2362 | Replies: 19
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC