7.16 LAB: Breakout room activity - Interactive Menu Learning Goals use a dictionary to store menu items use a function to print formatted menu options use a while loop to create an interactive program check the user input using if branches check that an option is correct (verify that a dictionary key exists) use break to interrupt the program execution Introduction In this lab, we will be building an application that uses an interactive menu. Let's say our high-level menu has the following options: L : List A : Add E : Edit D : Delete M: Show statistical data S : Save the data R : Restore data from file Q : Quit this program These key-option mappings will be stored in a dictionary in the main program. print_main_menu() function Write the print_main_menu() that accepts a dictionary of keys-options like the one shown above and prints the menu options stored in that dictionary in an easy-to-read format. Below is an example of the result of calling print_main_menu() (notice the question it asks at the top - it is part of the function output): Example Given the menu with the following options as mentioned above, the call to print_main_menu(main_menu) will output: ========================== What would you like to do? L - List A - Add E - Edit D - Delete M - Show statistical data S - Save the data R - Restore data from file Q - Quit this program ========================== Program flow The expected program flow is: The main program starts with a menu of options given above Loop indefinitely (while the user didn't choose to exit): Print the menu to the user Get the user's choice from input() Check if the user's choice is a valid option in the menu (is it one of the dictionary keys?). If the input is a valid option, print the option that user selected If not, simply continue from the top of the loop If the user entered 'Q', break the while loop Instructions Fix TODO 1: Add the options from the instructions to the_menu dictionary inside the main program. Fix TODO 2: Implement the "Quit" option, breaking from the while loop if the user input is an uppercase OR lowercase "Q". Fix TODO 3: Check whether a provided option is a valid menu option. Each time a valid menu option is provided, the program "echoes" it back to the user as follows: print(f"You selected option {opt} to > {the_menu[opt]}.") Please Use PYTHON def print_main_menu(menu): """ Given a dictionary with the menu, prints the keys and values as the formatted options. Adds additional prints for decoration and outputs a question "What would you like to do?" """ if __name__ == "__main__": the_menu = {} # TODO 1: add the options from the instructions opt = None while True: # print_main_menu(...) # TODO 1: uncomment, define the function, and call with the menu as an argument print("::: Enter an option") opt = input("> ") if opt == ...: # TODO 2: make Q or q quit the program print("Goodbye!\n") break # exit the main `while` loop else: if ...: # TODO 3: check of the character stored in opt is in the_menu dictionary print(f"You selected option {opt} to > {the_menu[opt]}.") else: print(f"WARNING: {opt} is an invalid option.\n")
7.16 LAB: Breakout room activity - Interactive Menu
Learning Goals
- use a dictionary to store menu items
- use a function to print formatted menu options
- use a while loop to create an interactive
program - check the user input using if branches
- check that an option is correct (verify that a dictionary key exists)
- use break to interrupt the program execution
Introduction
In this lab, we will be building an application that uses an interactive menu.
Let's say our high-level menu has the following options:
L : List A : Add E : Edit D : Delete M: Show statistical data S : Save the data R : Restore data from file Q : Quit this programThese key-option mappings will be stored in a dictionary in the main program.
print_main_menu() function
Write the print_main_menu() that accepts a dictionary of keys-options like the one shown above and prints the menu options stored in that dictionary in an easy-to-read format. Below is an example of the result of calling print_main_menu() (notice the question it asks at the top - it is part of the function output):
Example
Given the menu with the following options as mentioned above, the call to print_main_menu(main_menu) will output:
========================== What would you like to do? L - List A - Add E - Edit D - Delete M - Show statistical data S - Save the data R - Restore data from file Q - Quit this program ==========================Program flow
The expected program flow is:
- The main program starts with a menu of options given above
- Loop indefinitely (while the user didn't choose to exit):
- Print the menu to the user
- Get the user's choice from input()
- Check if the user's choice is a valid option in the menu (is it one of the dictionary keys?).
- If the input is a valid option, print the option that user selected
- If not, simply continue from the top of the loop
- If the user entered 'Q', break the while loop
Instructions
-
Fix TODO 1: Add the options from the instructions to the_menu dictionary inside the main program.
-
Fix TODO 2: Implement the "Quit" option, breaking from the while loop if the user input is an uppercase OR lowercase "Q".
-
Fix TODO 3: Check whether a provided option is a valid menu option.
Each time a valid menu option is provided, the program "echoes" it back to the user as follows:
print(f"You selected option {opt} to > {the_menu[opt]}.")def print_main_menu(menu):
"""
Given a dictionary with the menu,
prints the keys and values as the
formatted options.
Adds additional prints for decoration
and outputs a question
"What would you like to do?"
"""
if __name__ == "__main__":
the_menu = {} # TODO 1: add the options from the instructions
opt = None
while True:
# print_main_menu(...) # TODO 1: uncomment, define the function, and call with the menu as an argument
print("::: Enter an option")
opt = input("> ")
if opt == ...: # TODO 2: make Q or q quit the program
print("Goodbye!\n")
break # exit the main `while` loop
else:
if ...: # TODO 3: check of the character stored in opt is in the_menu dictionary
print(f"You selected option {opt} to > {the_menu[opt]}.")
else:
print(f"WARNING: {opt} is an invalid option.\n")
![LAB
ACTIVITY
1 def print_main_menu(menu):
2
3
4
5
6
7
8
9
7.16.1: LAB: Breakout room activity - Interactive Menu
10
11
Given a dictionary with the menu,
prints the keys and values as the
formatted options.
Adds additional prints for decoration
and outputs a question
"What would you like to do?"
main.py
0/13
Load default template...](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fccac55ee-732b-4607-a38e-f5bd0a8aacb2%2F343249c5-1208-4c91-b870-5653f80fb0fd%2Fo07zlqm_processed.png&w=3840&q=75)
![if_name_
the_menu
opt None
=
=
main ":
{} # TODO 1: add the options from the instructions
while True:
# print_main_menu(...) # TODO 1: uncomment, define the function, and call with the menu as an argument
print("::: Enter an option")
opt input("> ")
if opt
==
...: # TODO 2: make Q or q quit the program
print("Goodbye!\n")
break # exit the main `while Loop
else:
if ...: # TODO 3: check of the character stored in opt is in the menu dictionary
print (f"You selected option {opt} to > {the_menu[opt]}.")
print (f"WARNING: {opt} is an invalid option.\n")
else:](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fccac55ee-732b-4607-a38e-f5bd0a8aacb2%2F343249c5-1208-4c91-b870-5653f80fb0fd%2F3z31pr7_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)