python - 返回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
我的问题是,当我尝试在活动结束时将用户带回主菜单时,它会按预期打印菜单,但不允许用户输入选择,如果允许,它只是停止程序,而不是循环返回并再次正确运行它。
任何建议都会很棒,请提前致谢!
解决方案
试试下面的结构
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()
推荐阅读
- laravel - 如何纠正在 laravel tinker 的 psy shell 中找不到的类?
- c++ - NVCC 无法处理 MSVC 编译器选项中的嵌套引号
- php - JMSSerializerBundle 如何自定义将整数映射到字符串
- reporting-services - 如果没有详细信息行,SSRS 隐藏行组
- spring-boot - 如何在 SpringBoot Flux 中将客户端证书转发到下游服务
- python - tensor.shape 在使用 tf.keras 时返回一个无值列表
- typescript - TypeORM 使用 take 返回无效的结果数量
- amazon-web-services - 调用 UploadServerCertificate 时 AWS AccessDenied
- tomcat - 在 Tomcat 9.0.X catalina.out 日志中显示日期和时间
- c++ - 如何在 C++ 中将 void/null 作为默认参数添加到函数/lambda 指针?