猜数游戏用Python应该这样写
from random import randint
def guess():
#随机生成一个整数
value = randint(1,1000)
#最多允许猜5次
maxTimes = 5
for i in range(maxTimes):
prompt = 'Start to GUESS:' if i==0 else 'Guess again:'
#使用异常处理结构,防止输入不是数字的情况
try:
x = int(input(prompt))
#猜对了
if x == value:
print('Congratulations!')
elif x > value:
print('Too big')
else:
print('Too little')
except:
print('Must input an integer between 1 and 999')
else:
#次数用完还没猜对,游戏结束,提示正确答案
print('Game over. FAIL.')
print('The value is ', value)
guess()