猜数字(防沉迷改进版)
猜数字游戏,快来和我一起创作吧代码复制到IDLE中即可运行!
import random
try:
h=open("temp.txt")
temp=h.read()
h.close()
except FileNotFoundError:
h=open("temp.txt","w")
h.close
h=open("temp.txt","r+")
temp=h.read()
h.close()
if temp=='1':
a=input("你已经试过了,输入1即可再玩一次!")
try:
if a=="1":
f = open("temp.txt","r+")
f.write("2")
f.close()
secret = random.randint(1,10)
temp = input("不妨猜一下我现在心里想的是哪个数字(1到10):")
guess = int(temp)
times = 1
while (guess != secret) and (times < 3):
if guess > secret:
print("哥,大了大了~~~")
else:
print("嘿,小了小了~~~")
temp = input("请再试试吧:")
guess = int(temp)
times = times + 1
if times < 3:
print("哎呀,你是我心里的蛔虫吗?!")
print("哼,猜中了也没有奖励噢~")
input()
else:
print("唔,给三次机会都猜错,不跟你玩了!")
input()
else:
print("输入错误")
input()
except:
print("输入错误")
elif temp=="":
print("为响应国家防沉迷措施,最多玩两次哦!")
f = open("temp.txt","r+")
f.write("1")
f.close()
secret = random.randint(1,10)
temp = input("不妨猜一下我现在心里想的是哪个数字(1到10):")
guess = int(temp)
times = 1
while (guess != secret) and (times < 3):
if guess > secret:
print("哥,大了大了~~~")
else:
print("嘿,小了小了~~~")
temp = input("请再试试吧:")
guess = int(temp)
times = times + 1
if times < 3:
print("哎呀,你是我心里的蛔虫吗?!")
print("哼,猜中了也没有奖励噢~")
input()
else:
print("唔,给三次机会都猜错,不跟你玩了!")
input()
elif temp=='2':
print("超过游戏限制!")
input()
如果有bug,可以帮忙改进吗?谢谢! 感觉有点复杂了,看看原版的猜数游戏:
import random
print('在 1 ~ 100 之间猜一个数')
result = random.randint(1, 100)# 随机一个数
while True:
x = input('请猜一个数:')
x = int(x)
if x == result:
print('猜对了!游戏结束。')
break
elif x < result:
print('猜小了,请继续;', end='')
elif x > result:
print('猜大了,请继续;', end='')
页:
[1]