You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本启动后重复弹出主菜单的异常问题咨询

问题:Python脚本启动后重复显示主菜单的异常问题

启动脚本后会弹出两个主菜单,在第一个主菜单选择「1=Play a Game」后,会再次弹出主菜单,必须再次选择「1」才能进入预期的游戏菜单。

代码示例

def mainmenu():
    print("Welcome to the main menu!")
    print("1 = Play a Game")
    mmchoice = input("Choice:")
    return mmchoice

def gamemenu():
    print("Welcome to the game menu!")
    print("1 = Back")
    print("2 = Rock Paper Scissors")
    gmchoice = input("Choice:")
    return gmchoice

def mainmenuchoices():
    choice = mainmenu()
    if choice == "1":
        gamemenu()

mainmenu()
mainmenuchoices()

运行输出

Welcome to the main menu!
1 = Play a Game
Choice:1
Welcome to the main menu!
1 = Play a Game
Choice:1
Welcome to the game menu!
1 = Back
2 = Rock Paper Scissors
Choice:

问题原因

你同时调用了mainmenu()和mainmenuchoices()两个函数,而mainmenuchoices()内部又主动调用了一次mainmenu(),导致启动时连续触发两次主菜单的渲染逻辑。

解决方法

删除单独的mainmenu()调用,只保留mainmenuchoices()的调用即可。修改后的代码如下:

def mainmenu():
    print("Welcome to the main menu!")
    print("1 = Play a Game")
    mmchoice = input("Choice:")
    return mmchoice

def gamemenu():
    print("Welcome to the game menu!")
    print("1 = Back")
    print("2 = Rock Paper Scissors")
    gmchoice = input("Choice:")
    return gmchoice

def mainmenuchoices():
    choice = mainmenu()
    if choice == "1":
        gamemenu()

# 仅保留这一行调用即可触发完整流程
mainmenuchoices()

修改后启动脚本只会显示一次主菜单,选择「1」后可直接进入游戏菜单。

内容的提问来源于stack exchange,提问作者NtownOMG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:56:06