猜数字
使用random模块的randint()方法生成一个随机数,使用input方法让用户进行输入数字,当前输入的数字与生成的随机数一致时提示游戏结束,输入超过三次都错误时,游戏也结束
import random
c = random.randint(1,10)
#引入一个随机数并将其赋值给c(1到10的随机数)
a = input("请输入")
t = int(a)
#让用户输入一个数字
d = 1
#计算用户输入次数
while t != c:
#循环,如果输入的数不等于随机数,则让用户重新输入
if d < 3:#如果输入的次数小于3,则进行游戏,如何大于3,则退出循环
if t < c:
print("小了")
else:
print("大了")
a = input("请再次输入")
t = int(a)
d = d + 1
else:
break
if d <=3 and t == c:
print("输入正确,游戏结束")
else:
print("输入次数过多,游戏结束")