[Tutor] Guess Your Number Game

Jon Moore jonathan.r.moore at gmail.com
Tue Jan 17 12:04:20 CET 2006


Hi

I hope someone can help me!

I am currently learning Python using a book by Michael Dawson. In one of the
exercises I have to right a program that will guess a number chosen by the
user.

It is partly working, however it does not seem to keep state of numbers that
should have already been ruled out as too high or low.

Any pointers would be very much appreciated!

import random

print "Welcome to 'Guess Your Number'!"
print "\nThink of a number between 1 and 100."
print "And I will try and guess it!\n"
print "Valid inputs are: higher, lower and correct."

raw_input("\n\nPress enter once you have thought of a number.")


# set the initial values
guess = random.randrange(100) + 1
tries = 1



# guessing loop
response = ""
while response != "correct":
    print "Is it" ,guess, "?\n"
    response = raw_input ("")
    if response == "lower":
        guess = random.randrange(1, guess)
    elif response == "higher":
        guess = random.randrange(guess, 100)


# Error message for invalid inputs
    else:
        print "Invalid entry!"


    tries += 1


print "\nI guessed it!  The number was", guess
print "And it only took me", tries, "tries!\n"


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

--
Best Regards

Jon

--
Best Regards

Jon Moore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060117/ab245511/attachment.html 


More information about the Tutor mailing list