【问题描述】
猜数游戏:在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太小了”;小于预设的数,显示“遗憾,太小了”,如此循环,直至猜中该数,显示“预测N次,你猜中了!”,其中N是用户输入数字的次数。
【算法代码】
from random import randint
s=0
try:
x=randint(0,9)
while 1:
t=int(eval(input('Please input a digit:')))
if t<0 or t>9:
raise ValueError
if t<x:
print('Small')
s+=1
elif t>x:
print('Big')
s+=1
elif t==x:
s+=1
print("Good, you guess {} times!".format(s))
break
except (ValueError,NameError,SyntaxError):
print("Please enter a number between 0 and 9.")