# [Tutor] Guess my number game

Murtog murtog at gmail.com
Fri Dec 16 00:42:54 CET 2005

```Try this code:

#The Guess My Number Game
#
#The computer picks a random number between 1 and 50
#The player tries to guess it and the computer lets
#the player know if the guess is too high, too low
#or right on the money
#If the player fails to guess the number after 5 tries
#the game ends and the computer prints a chastising message
#
import random

print """\t\t\tWelcome to \"Guess My Number\"!
\nThink of a number between 1 and 50.
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 (50) + 1
tries = 1
useds = []

#FIXED
while (guess != number):
if (guess > number):
print "You chose", guess, "the number is Lower ..."
useds.append(guess)
else:
print "You chose", guess, "the number is Higher ..."
useds.append(guess)

guess = random.randrange (50) + 1
if guess in useds:
while guess in useds:
guess = random.randrange(50) + 1
else:
tries += 1

print "You guessed it! The number was", number
print "And it only took you", tries, "tries!\n"

raw_input ("Press <ENTER> to exit.")

It should work now. Cheers! =]

On 12/15/05, William Mhlanga <reddazz at gmail.com> wrote:
>
> I have been trying to write a guess my number game (using Michael Dawsons
> book), where the computer guesses the number that I thought of. Here is my
> code so far,
> #The Guess My Number Game
> #
> #The computer picks a random number between 1 and 50
> #The player tries to guess it and the computer lets
> #the player know if the guess is too high, too low
> #or right on the money
> #If the player fails to guess the number after 5 tries
> #the game ends and the computer prints a chastising message
> #
>
> print "\t\t\tWelcome to \"Guess My Number\"!"
> import random
>
> print "\nThink of a number between 1 and 50."
> 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 (50) + 1
> tries = 1
>
> #FIXME
> while (guess != number):
>         if (guess > number):
>                 print "You chose", guess, "the number is Lower ..."
>         else:
>                 print "You chose", guess, "the number is Higher ..."
>         guess = random.randrange (50) + 1
>         tries += 1
>
> print "You guessed it! The number was", number
> print "And it only took you", tries, "tries!\n"
>
> raw_input ("Press <ENTER> to exit.")
>
> The program works ok, but the computer sometimes repeats the same numbers
> when asked to guess. How can I rewrite it so that it guesses like what a
> human being does i.e. if the number is less than 20, you do not guess
> numbers above 20.
>
> Thanks for your help.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>

--
\(^_^)/
Murtog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051215/ce445fd5/attachment.htm
```