鱼C论坛

 找回密码
 立即注册
查看: 2278|回复: 1

[作品展示] 猜数字(防沉迷改进版)

[复制链接]
发表于 2022-8-31 14:13:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
猜数字游戏,快来和我一起创作吧
代码复制到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,可以帮忙改进吗?谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-31 18:04:09 | 显示全部楼层
感觉有点复杂了,看看原版的猜数游戏:
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='')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 16:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表