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

Python菜单与子菜单实现问题:子菜单选项无法显示

解决带子菜单的Python菜单问题

先看你代码里的几个关键错误:

  • display_header()函数定义开头多了type,属于语法错误,应该是def display_header():
  • invitees_menu()函数开头有错误的递归调用和缩进问题,导致子菜单内容永远不会被执行
  • 获取子菜单选择时,lower是字符串方法,需要加括号lower(),否则返回的是方法对象而非小写字符串
  • 子菜单的while循环逻辑颠倒,应该先打印菜单再处理用户选择
  • 子菜单中选择"v"时错误调用了drinks_menu(),应该是对应查看邀请人的函数
  • 子菜单选择"b"时没有退出循环,无法回到主菜单

修正后的完整代码:

def display_header():
    main = "Main Menu"
    txt = main.center(90, ' ')
    print('{:s}'.format('\u0332'.join(txt)))
    print("Please choose an option from the following menu:")
    print("I. Invitee's Information")
    print("F. Food Menu")
    print("D. Drinks Menu")
    print("P. Party Items Menu")
    print("Q. Exit")

def get_user_choice():
    choice = input("Enter your choice: ").strip().lower()
    return choice

# 定义子菜单需要用到的空函数,避免运行报错
def enter_invitee():
    print("Adding new invitee...")

def edit_invitee():
    print("Editing invitee...")

def view_invitees():
    print("Viewing all invitees...")

def food_menu():
    print("Food Menu placeholder...")

def drinks_menu():
    print("Drinks Menu placeholder...")

def party_menu():
    print("Party Items Menu placeholder...")

def invitees_menu():
    while True:
        # 先打印子菜单内容
        print("\nInvitees' Information Menu")
        print("Please choose an option from the following menu:")
        print("A. Add new invitee information")
        print("E. Edit existing invitee information")
        print("V. View all invitees")
        print("B. Go back to main menu")
        
        # 获取用户选择并转为小写
        choice = input("Enter your sub-menu choice: ").strip().lower()
        
        # 处理选择逻辑
        if choice == "a":
            enter_invitee()
        elif choice == "e":
            edit_invitee()
        elif choice == "v":
            view_invitees()  # 修正之前错误调用drinks_menu的问题
        elif choice == "b":
            display_header()  # 返回主菜单前重新显示主菜单
            break  # 退出子菜单循环,回到主菜单
        else:
            print("Invalid choice, please try again.")

if __name__ == "__main__":
    display_header()
    while True:
        choice = get_user_choice()
        if choice == "i":
            invitees_menu()
        elif choice == "f":
            food_menu()
        elif choice == "d":
            drinks_menu()
        elif choice == "p":
            party_menu()
        elif choice == "q":
            print("Thank you for using the program!")
            break
        else:
            print("Invalid choice, please try again.")

关键修改说明:

  1. 移除display_header()定义前的type,修复语法错误
  2. 重构invitees_menu()的逻辑:先打印子菜单,再获取用户选择,处理完选择后通过break退出子菜单循环
  3. 修正lower()的调用方式,确保获取到小写的选择字符串
  4. 修正子菜单中"v"选项的函数调用,从drinks_menu()改为对应查看邀请人的view_invitees()
  5. 给主菜单和子菜单添加了无效选择的提示,提升用户体验
  6. 给未实现的函数添加了占位定义,避免运行时出现未定义错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 01:20:41