程序


# while-else
# Simple guessing game: start with a random number and
# guess with hints until:
#    guess is  correct
#    the guess is out of range indicating the user is quitting

import random  # get the random number module
number = random.randint(0,100) # get a random number
# between 0 and 100 inclusive

print "Hi,this is the guess the number game. You can enter the number between 0 to 100 to guess it"
print number

guessStr=raw_input("Please enter the number (0~100):")
guess=int(guessStr)
while 0<= guess <=100:
if guess > number:
print "Too high. \n"
elif guess < number:
print "Too low \n"
else:
print "You get the answer. The number is", number
break

guessStr=raw_input("Please enter the number (0~100):")
guess=int(guessStr)

else:
#if the input is not in 0 to 100
print "You quit too early.The number is:", number



效果

Written on January 13, 2013