Reputation: 25
I've been working on this all day and cannot figure out what I'm not understanding This simple program should ask for user input until they enter 9 and then the program exits.
There are 3 functions total. def main():, def menu():, and def menuInput():
1)def main():-simply calls menuInput
2)def menu():-displays a menu only
3)def menuInput():-calls the menu to be displayed, accepts user input, tests to make sure a valid number has been entered, Continues to ask the user for input until a valid choice is made, Repeats the menu selection until 9 is selected.
The problem is that the entire menu should be displayed, and then the program asks for the users selection 1-9. If 1 is chosen, it displays the message and shows the menu again, asking for the input.
Here is my code so far with comments based on my understanding or lack of
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()
Upvotes: 0
Views: 1711
Reputation: 44
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()
You forgot to add exit(1)
Upvotes: 1