鱼C论坛

 找回密码
 立即注册
查看: 1653|回复: 2

[已解决]有没有更好的解决办法?

[复制链接]
发表于 2023-4-28 23:58:31 | 显示全部楼层 |阅读模式

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

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

x
先看看代码:
import random
import easygui as eg
def bbb():
    secret = random.randint(1, 10)
    msg = '不妨猜一下我现在心里想的数字(1~10):'
    title = '数字小游戏'
    guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    while True:
        if guess == secret:
            eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
            eg.msgbox('猜中了也没有奖励')
            break
        else:
            if guess > secret:
                eg.msgbox('大了大了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
            else:
                eg.msgbox('小了小了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    重试 = eg.buttonbox(msg='是否重来?', title='重试', choices=('是', '否'))
    if 重试 == '是':
        bbb()
    else:
        eg.msgbox('游戏结束')
def aaa():
    eg.msgbox('嗨,欢迎进入第一个界面小游戏!')
    secret = random.randint(1, 10)
    msg = '不妨猜一下我现在心里想的数字(1~10):'
    title = '数字小游戏'
    guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    while True:
        if guess == secret:
            eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
            eg.msgbox('猜中了也没有奖励')
            break
        else:
            if guess > secret:
                eg.msgbox('大了大了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
            else:
                eg.msgbox('小了小了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    重试 = eg.buttonbox(msg= '是否重来?', title='重试', choices=('是','否'))
    if 重试 == '是':
        bbb()
    else:
        eg.msgbox('游戏结束')
aaa()
我是想在第一次游戏结束后选择重试后再次运行代码,但是重试时我不想再出现:eg.msgbox('嗨,欢迎进入第一个界面小游戏!')这句,
想了好久,只好用笨方法了。
最佳答案
2023-4-29 00:02:15
可以通过在函数 aaa() 中添加一个参数来控制是否显示欢迎信息。这样,你就可以在重试时避免重复显示欢迎信息。下面是修改后的代码:
import random
import easygui as eg

def game(show_welcome=True):
    if show_welcome:
        eg.msgbox('嗨,欢迎进入第一个界面小游戏!')
    
    secret = random.randint(1, 10)
    msg = '不妨猜一下我现在心里想的数字(1~10):'
    title = '数字小游戏'
    guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    
    while True:
        if guess == secret:
            eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
            eg.msgbox('猜中了也没有奖励')
            break
        else:
            if guess > secret:
                eg.msgbox('大了大了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
            else:
                eg.msgbox('小了小了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    
    重试 = eg.buttonbox(msg='是否重来?', title='重试', choices=('是', '否'))
    if 重试 == '是':
        game(show_welcome=False)  # 重试时不显示欢迎信息
    else:
        eg.msgbox('游戏结束')

game()

现在,当你第一次运行游戏时,会显示欢迎信息。但是在重试时,将不会再显示欢迎信息。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-29 00:02:15 | 显示全部楼层    本楼为最佳答案   
可以通过在函数 aaa() 中添加一个参数来控制是否显示欢迎信息。这样,你就可以在重试时避免重复显示欢迎信息。下面是修改后的代码:
import random
import easygui as eg

def game(show_welcome=True):
    if show_welcome:
        eg.msgbox('嗨,欢迎进入第一个界面小游戏!')
    
    secret = random.randint(1, 10)
    msg = '不妨猜一下我现在心里想的数字(1~10):'
    title = '数字小游戏'
    guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    
    while True:
        if guess == secret:
            eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
            eg.msgbox('猜中了也没有奖励')
            break
        else:
            if guess > secret:
                eg.msgbox('大了大了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
            else:
                eg.msgbox('小了小了')
                guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
    
    重试 = eg.buttonbox(msg='是否重来?', title='重试', choices=('是', '否'))
    if 重试 == '是':
        game(show_welcome=False)  # 重试时不显示欢迎信息
    else:
        eg.msgbox('游戏结束')

game()

现在,当你第一次运行游戏时,会显示欢迎信息。但是在重试时,将不会再显示欢迎信息。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-29 00:10:01 | 显示全部楼层
isdkz 发表于 2023-4-29 00:02
可以通过在函数 aaa() 中添加一个参数来控制是否显示欢迎信息。这样,你就可以在重试时避免重复显示欢迎信 ...

多谢大佬!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-23 11:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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