首页 > 解决方案 > 返回python中的主菜单

问题描述

只是一个快速的(我对 Python 编码非常陌生,但如果有帮助的话,我有一年左右的 R 经验)。

我在下面打印了我的编码,其中有一个菜单,用户将在其中选择一个选项,将他们引导到某个活动(A、B、C、D、X),它比我发布的内容更多,但我不想提交更大的文本代码墙。

def printMenu ():
    print("Playing Statistics Calculator")
    print("A: Positions in Basketball and relevent Key Performance Indicators")
    print("B: Calculate your per-game statistics")
    print("C: Compare your statistics to other players in your position")
    print("X: Exit")

def main():
    choice = printMenu()
    choice

main()

selection = input("Please choose a selection: ")

if selection == "A":
    print("You are interested in looking at the Key Performance Indicators (KPIs) that we think are important for each position. Please select a position below:")
    main()
    selection
elif selection == "B":
    print("Now you know the important KPIs related to each position, which position are you interested in, in our team?")
    main()
    selection
elif selection == "D":
    print("comparison calculations in here")
    main()
    selection
elif selection == "X":
    exit()
else:
    print("Try again, please ensure the letters are in capitals and are shown in menu")
    main()
    selection

我的问题是,当我尝试在活动结束时将用户带回主菜单时,它会按预期打印菜单,但不允许用户输入选择,如果允许,它只是停止程序,而不是循环返回并再次正确运行它。

任何建议都会很棒,请提前致谢!

标签: pythonmenu

解决方案


试试下面的结构

def printMenu():
    print("Playing Statistics Calculator")
    print("A: Positions in Basketball and relevent Key Performance Indicators")
    print("B: Calculate your per-game statistics")
    print("C: Compare your statistics to other players in your position")
    print("X: Exit")
    return input("Please choose a selection: ").upper()

def program(selection):
    if selection == "A":
        print("You are interested in looking at the Key Performance Indicators (KPIs) that we think are important for each position. Please select a position below:")
    elif selection == "B":
        print("Now you know the important KPIs related to each position, which position are you interested in, in our team?")
    elif selection == "C":
        print("comparison calculations in here")
    else:
        print("Try again, please ensure the letter is shown in the menu.")

selection = printMenu()
while selection != 'X':
    program(selection)
    print()
    selection = printMenu()     

推荐阅读