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

Python多语言早安问候菜单程序运行无报错仅显示空白shell求助

问题排查及修复

核心错误点如下:

  • 无程序执行入口:Python不会自动执行自定义的mainMenu函数,你需要在所有函数定义完成后显式调用mainMenu()启动程序,这是窗口完全空白的直接原因。
  • 赋值运算符误用:获取用户输入的代码行用了双等号==(用于值比较),正确应该用单等号=给selection变量赋值,否则变量未定义后续逻辑会报错。
  • 无效退出逻辑:函数末尾单独写的exit不是有效的调用语句,不会触发程序退出。
  • 递归调用风险:每次返回菜单都递归调用mainMenu(),运行次数多了会触发栈溢出,不建议用递归做菜单跳转。
修复后完整代码
def mainMenu():
    print("Select a language and I Will Say Good Morning\n")
    print("1. English")
    print("2. Italian")
    print("3. Spanish")
    print("4. German")
    print("5. End the Program\n")
    while True:
        try:
            # 修正为赋值运算符
            selection = int(input("Enter your selection# "))
            if selection == 1:
                goodmorning()
            elif selection == 2:
                buongiorno()
            elif selection == 3:
                buenosdias()
            elif selection == 4:
                gutenmorgen()
            elif selection == 5:
                # 退出循环结束程序
                break
            else:
                print("Invalid choice. Enter 1-5.")
        except ValueError:
            print("Invalid choice! Choose 1-5!!")

def goodmorning():
    print("\nGood Morning")
    input("Enter anything to return to the Main Menu\n")

def buongiorno():
    print("\nBuongiorno")
    input("Enter anything to return to the Main Menu\n")

def buenosdias():
    print("\nBuenos Dias")
    input("Enter anything to return to the Main Menu\n")

def gutenmorgen():
    print("\nGuten morgen")
    input("Enter anything to return to the Main Menu\n")

# 添加入口调用
if __name__ == "__main__":
    mainMenu()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 08:15:04