[Tutor] guess my number game

Alexander Kapshuk akap at isd.dp.ua
Tue Mar 27 12:16:35 CEST 2007


I'm working on a program that has the user think of a number between 1
and 100 and then tries to guess that number.

 

I'm having trouble telling the computer to keep on looking for the
correct number, each time narrowing down the search range.

 

Please see the code below.

 

import random

 

print "\tWelcome to 'Guess My Number 1.2'!"

print "\nThink of a number between 1 and 100."

print "The computer will try to guess it in as few attempts as
possible.\n"

 

# set the initial values

user_number = int(raw_input("Think of a number between 1 and 100 and
press Enter: "))

guess = random.randrange(50) + 1

answer = ""

tries = 1

 

# guessing loop

print guess

answer = raw_input("Is the above No '>', '<' or '=' as your No?: ")

while (answer != "="):

    if (answer == ">"):

        print (guess = random.randrange(100) + 51)

    elif (answer == "<"):

        print (guess = random.randrange(49) + 1)

    elif (answer == "="):

        print "Correct! The number was", user_number "And it only took
you", tries " tries!\n"

    else:

        print "Keep on trying!"

 

    tries += 1

 

raw_input("\n\nPress the enter key to exit.")

 

 

 

 

 

Thanking you all in advance.

 

 

Alexander Kapshuk

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070327/4dbfbac1/attachment.html 


More information about the Tutor mailing list