【问题标题】:How do I display the menu properly after each user input?每次用户输入后如何正确显示菜单?
【发布时间】:2020-03-12 16:31:58
【问题描述】:

我整天都在研究这个问题,但无法弄清楚我不理解什么 这个简单的程序应该要求用户输入,直到他们输入 9 然后程序退出。

共有 3 个功能。 def main():, def menu():, and def menuInput():

1)def main():-只调用menuInput

2)def menu():-只显示一个菜单

3)def menuInput():-调用要显示的菜单,接受用户输入,测试以确保输入了有效的数字,继续要求用户输入直到做出有效的选择,重复菜单选择直到选择 9。

问题是应该显示整个菜单,然后程序要求用户选择 1-9。如果选择 1,它会显示消息并再次显示菜单,要求输入。

根据我的理解或缺乏,这是迄今为止我使用 cmets 的代码

def main():
#calls menu input
  menuInput()

def menu():
#assigns the menu to menuDisplay
  menuDisplay = print('''
  Welcome! Please make a choice from the following menu

  1. Select a year and display available data
  2. Review averages by year range
  3. Select a date range to display highest
  4. Select a date range to display lowest 
  5. Get total for a selected year range
  6. blank
  7. blank
  8. See this menu again
  9. QUIT the program
''')


def menuInput():
#loops until 9 is selected  
while True:
    try:
      userChoice=int(input('Please make a selection: '))
    except ValueError:
    print('Please enter a whole number less than or equal to 9')

  if userChoice > 9:
    print('Please enter a number less or equal to 9')
  elif userChoice == 0:
    print('Please enter a number greater than 0')
  elif userChoice == 1:
    print('Good')
  elif userChoice == 2:
    print('Good')
  elif userChoice == 3:
    print('Good')
  elif userChoice == 4:
    print('Good')
  elif userChoice == 5:
    print('Good')
  elif userChoice == 6:
    print('Good')
  elif userChoice == 7:
    print('Invalid Choice')
  elif userChoice == 8:
    print('Good')
  else:
    print('Thank you! Program Exiting!')

#Calls main 
main()

【问题讨论】:

  • 你没有描述问题是什么,但是你的except:ValueError这行不正确,应该是except ValueError:

标签: python


【解决方案1】:
def main():
#calls menu input
  menuInput()

def menu():
#assigns the menu to menuDisplay
  menuDisplay = print('''
  Welcome! Please make a choice from the following menu

  1. Select a year and display available data
  2. Review averages by year range
  3. Select a date range to display highest
  4. Select a date range to display lowest 
  5. Get total for a selected year range
  6. blank
  7. blank
  8. See this menu again
  9. QUIT the program
''')


def menuInput():
#loops until 9 is selected  
    while True:
        menu()
        try:
            userChoice=int(input('Please make a selection: '))
        except ValueError:
            print('Please enter a whole number less than or equal to 9')

        if userChoice > 9:
            print('Please enter a number less or equal to 9')
        elif userChoice == 0:
            print('Please enter a number greater than 0')
        elif userChoice == 1:
            print('Good')
        elif userChoice == 2:
            print('Good')
        elif userChoice == 3:
            print('Good')
        elif userChoice == 4:
            print('Good')
        elif userChoice == 5:
            print('Good')
        elif userChoice == 6:
            print('Good')
        elif userChoice == 7:
            print('Invalid Choice')
        elif userChoice == 8:
            print('Good')
        else:
            print('Thank you! Program Exiting!')
            exit(1)

#Calls main 
main()

您忘记添加 exit(1)

【讨论】:

  • 这似乎有所帮助,但每次输入后都应显示整个菜单,除非用户输入 9,然后程序显示“谢谢!程序退出”
  • 您的显示菜单的menu 函数永远不会在您的代码中调用。你可以在你的 while 循环开始时调用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
相关资源
最近更新 更多