times = 3
#导入 random 组件,用于生成随机数
import random
#生成一个1-10的随机数
x = random.randint(1,10)
while times > 0:
temp =input("猜一下所想的是哪个数字:")
guess = int(temp)
if guess == x:
print("对了")
break
else:
if guess < x:
print("小了")
else:
print("大了")
times = times-1
print("剩余游戏次数:",times)
print("游戏结束\n")
运行结果