返回python中的主菜单

返回python中的主菜单,python,menu,Python,Menu,只是一个简单的例子(我对Python编码非常陌生,但如果有帮助的话,我有一年左右的R编程经验) 我在下面有一个我的编码打印输出,在这里我有一个菜单,用户可以选择一个选项,引导他们进入某个活动(a、B、C、D、X),这比我发布的内容更为重要,但我不想提交更大的文本代码墙 def printMenu (): print("Playing Statistics Calculator") print("A: Positions in Basketball a

只是一个简单的例子(我对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
我的问题是,当我试图在活动结束时将用户带回主菜单时,它会按预期打印菜单,但不允许用户输入选择,如果允许,它只会停止程序,而不是循环并再次正确运行


任何建议将是伟大的,请提前感谢

尝试以下结构

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()     

尝试以下结构

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()     

问题是,当您对else/if语句调用“selection”时,它不会返回到处理输入,因此我认为最好的选择是将该部分也定义为函数,并在必要时调用它

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()

def processInput()
    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()
        processInput()
    elif selection == "B":
        print("Now you know the important KPIs related to each position, which position are you interested in, in our team?")
        main()
        processInput()
    elif selection == "D":
        print("comparison calculations in here")
        main()
        processInput()
    elif selection == "X":
        exit()
    else:
        print("Try again, please ensure the letters are in capitals and are shown in menu")
        main()
        processInput()

processInput()

我会这样做,尽管您应该做最适合您的程序的事情,但问题是,当您在else/if语句上调用“selection”时,它不会返回到处理输入,因此我认为最好的选择是将该部分也定义为函数,并在必要时调用它

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()

def processInput()
    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()
        processInput()
    elif selection == "B":
        print("Now you know the important KPIs related to each position, which position are you interested in, in our team?")
        main()
        processInput()
    elif selection == "D":
        print("comparison calculations in here")
        main()
        processInput()
    elif selection == "X":
        exit()
    else:
        print("Try again, please ensure the letters are in capitals and are shown in menu")
        main()
        processInput()

processInput()

我会这样做,尽管您应该做最适合您的程序的事情

if-else
部分中
选择
意味着什么?它什么也不做。在
if-else
部分中,
选择
意味着什么?它没有任何作用。谢谢!这个很好用,谢谢!这很好用谢谢丹尼尔,这也很好用!谢谢丹尼尔,这也很好用!