from random import randint
def guess():
num_int = randint(0, 10)
while True:
user_input = input("what number you guess (1, 10)")
try:
user_guess = int(user_input)
if user_guess == num_int:
print("u r right, the number is %s" % user_guess)
break
elif user_guess < num_int:
print("the number is greater then yours , do not give up")
else:
print("the number is less then yours , do not give up")
except:
print("input error")
if __name__ == '__main__':
guess()
猜0~10 还是比较容易的,如果猜0~100 就麻烦些,可以加入尝试次数的限制,增加可玩性
来源:oschina
链接:https://my.oschina.net/dkexcellent/blog/3198316