在 python 中返回主菜单
Returning back to main menu in python
只是一个快速的(我对 Python 中的编码非常陌生,但如果有帮助的话,我有一年左右的 R 经验)。
我在下面打印了我的编码,其中有一个菜单,用户可以在其中 select 一个选项,将他们引导到某个 activity(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
我的问题是,当我尝试在 activity 结束时将用户带回主菜单时,它会按预期打印菜单,但不允许用户输入 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()
问题是,当您在 else/if 语句中调用“选择”时,它不会返回处理输入,所以我认为最好的选择是将该部分定义为函数也一样,需要的时候调用
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()
我会做这样的事情,尽管你应该做最适合你的程序的事情
只是一个快速的(我对 Python 中的编码非常陌生,但如果有帮助的话,我有一年左右的 R 经验)。
我在下面打印了我的编码,其中有一个菜单,用户可以在其中 select 一个选项,将他们引导到某个 activity(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
我的问题是,当我尝试在 activity 结束时将用户带回主菜单时,它会按预期打印菜单,但不允许用户输入 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()
问题是,当您在 else/if 语句中调用“选择”时,它不会返回处理输入,所以我认为最好的选择是将该部分定义为函数也一样,需要的时候调用
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()
我会做这样的事情,尽管你应该做最适合你的程序的事情