[Tutor] Guess Your Number Game
Edgar Antonio Rodríguez Velazco
edgar.antonio.rv at gmail.com
Tue Jan 17 23:02:18 CET 2006
Check this:
###########################################################
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
x = 1 ## Pay atention to these two variables.
y = 100
guess = random.randrange(x, y) ## Now we use them
tries = 1
# guessing loop
response = ""
while response != "correct":
print "Is it" ,guess, "?\n"
response = raw_input ("")
if response == "lower":
y = guess ## y is the 100 above, now, we take the guess as "y"
and "y" as the maximum value of the parameters of guess.
guess = random.randrange(x, y)
elif response == "higher":
x = guess
guess = random.randrange(x, y) ## we take guess as the minimun value
of our guess and plug it into x
# 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.")
####################################################
I hope this can help you. ---- I am newbie, so if I did a mistake I hope you
to understand my self ;) ---
--
Edgar A. Rodriguez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060117/e86c241f/attachment.html
More information about the Tutor
mailing list