python 猜数字小游戏
变量不需要事先声明
变量的类型不需要声明
##静态数字小游戏####
whit True :
ran = 18
s = 3
for jin range(10):
print("请输入一个“1-99”的数字猜一猜~")
x=input(">>>:")
x=int(x)
if x == ran:
print("恭喜你猜对了,鼓励鼓励~~~")
print("是否再来一次? [yes/no]")
yes =input()
if yes =="yes" or yes == "Y" or yes == "y" :
s =3
else:
exit()
elif x > ran:
s -=1
print("数字太大再来一次吧,您还剩余",s,"次机会")
else:
s -=1
print("你猜的数字太小了再来一次吧,你当前剩余", s,"次机会")
if s ==0 :
print("对不起您当前可用猜测次数为 ",s," 重新开始[yes/no]")
sun=input(">>>:")
if sun !="yes" :
exit()
else:
s =3
####猜数字小游戏动态##########
#加载随机数模块
import random
# 循环10次 可更改
for i in range(10):
#获取随机数
ran = random.randint(1,99)
s = 3 #初始化次数
for j in range(10):
print("请输入一个“1-99”的数字猜一猜~")
x=int(input(">>>:")) #获取键盘输入
if x == ran:
print("恭喜你猜对了,鼓励鼓励~~~")
print("是否再来一次? [yes/no]")
yes = input()
if yes == "yes" or yes == "Y" or yes == "y" :
s = 3
else:
exit()
elif x > ran:
s -= 1
print("数字太大再来一次吧,您还剩余",s,"次机会")
else:
s -= 1
print("你猜的数字太小了再来一次吧,你当前剩余", s, "次机会")
if s == 0 :
print("对不起您当前可用猜测次数为 ",s," 重新开始[yes/no]")
sun=input(">>>:")
if sun != "yes" :
exit()
else:
s = 3
#####精简易看版###################
import sys
s = 0
while s < 3:
i = int(input("请输入:").strip())
s += 1
if i == 3:
print("正确")
sys.exit()
if i > 3:
print("大了")
if i < 3:
print("小了")
if s == 3:
while True:
inu = input("是否继续? [y/n] >>:")
if inu == "y":
s = 0
break
elif inu == "n" :
sys.exit()
网友评论