我对这三个功能不了解什么?

问题描述 投票:0回答:1

我整天都在工作,无法弄清我不了解的内容这个简单的程序应该要求用户输入,直到他们输入9,然后程序退出。

共有3个功能。 def main():、 def menu():和def menuInput():

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

[2)def menu():-仅显示菜单

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

问题是应该显示整个菜单,然后程序要求用户选择1-9。如果选择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:
    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()
python
1个回答
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:
        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()

您忘记添加出口(1)

© www.soinside.com 2019 - 2024. All rights reserved.