import random
a = int(input("请输入随机数范围下限(即最小值·只能是整数):"))
b = int(input("请输入随机数范围上限(即最大值·只能是整数):"))
secret = random.randint(a,b)
time = int(input("请输入猜数字的最多次数:"))
guess = a - 1
print("-----------欢迎来到猜数字游戏·请开始--------")
while guess!=secret and time >=0:
guess=int(input("数字区间%d-%d·请输入你猜的数字:"%(a,b)))
if guess == secret:
print("恭喜你·猜对了!")
else:
print("太遗憾了·你猜错了·你还有",time,"次机会")
if guess<secret:
a=guess
print("你猜的数字小于正确答案")
print("现在的数字区间是:",a,"-",b)
else:
b=guess
print("你猜的数字大于正确答案")
print("现在的数字区间是:",a,"-",b)
time -= 1
print("Game over")
原文:https://www.cnblogs.com/ShanXie/p/13569531.html