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")

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

  1. Fix TODO 1: Add the options from the instructions to the_menu dictionary inside the main program.

  2. Fix TODO 2: Implement the "Quit" option, breaking from the while loop if the user input is an uppercase OR lowercase "Q".

  3. 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")

**Lab Activity: Breakout Room Activity - Interactive Menu (7.16.1)**

**File: main.py**

The image shows the beginning of a Python script that defines a function called `print_main_menu`. Below is a transcription of the code and comments present:

```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?"
    """
```

**Explanation of Code:**

- **Function Definition:** The function `print_main_menu` takes one parameter, `menu`, which is expected to be a dictionary.
- **Docstring:**
  - The function is designed to:
    - Print the keys and values of the `menu` dictionary in a formatted manner as menu options.
    - Add decorative prints to enhance the appearance.
    - Prompt the user with the question: "What would you like to do?"

**Contextual Notes:**

- This script is part of a lab activity focusing on creating interactive menus using Python.
- Students are likely expected to complete the function implementation and ensure it works with a provided or custom dictionary.

The screenshot also includes navigation elements such as a "Lab Activity" breadcrumb and an indication of progress (0/13), but no graphs or additional diagrams are shown.
Transcribed Image Text:**Lab Activity: Breakout Room Activity - Interactive Menu (7.16.1)** **File: main.py** The image shows the beginning of a Python script that defines a function called `print_main_menu`. Below is a transcription of the code and comments present: ```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?" """ ``` **Explanation of Code:** - **Function Definition:** The function `print_main_menu` takes one parameter, `menu`, which is expected to be a dictionary. - **Docstring:** - The function is designed to: - Print the keys and values of the `menu` dictionary in a formatted manner as menu options. - Add decorative prints to enhance the appearance. - Prompt the user with the question: "What would you like to do?" **Contextual Notes:** - This script is part of a lab activity focusing on creating interactive menus using Python. - Students are likely expected to complete the function implementation and ensure it works with a provided or custom dictionary. The screenshot also includes navigation elements such as a "Lab Activity" breadcrumb and an indication of progress (0/13), but no graphs or additional diagrams are shown.
The provided script is a Python program designed for a command-line menu interface. The script will likely appear on an educational website to demonstrate basic control flow and input handling in Python. Below is a transcription and explanation of the code:

```python
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 if 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")
```

### Detailed Explanation:

1. **Initialization**:
   - `the_menu` is a dictionary intended to hold menu options. It’s currently empty and requires filling as per instructions.
   - `opt` is initialized as `None` to store user input.

2. **Main Loop**:
   - A `while True` loop continuously runs, allowing users to interact with the menu until a specific condition is met to exit.

3. **User Input**:
   - The program prompts the user to enter an option with `print("::: Enter an option")` followed by `input("> ")` to capture input.

4. **Condition for Quitting**:
   - A placeholder condition `if opt == ...:` is set up for quitting the program. The developer should replace `...` with conditions like if `opt` is 'Q' or 'q'.

5. **Menu Selection**:
   - The code checks if the input `opt` exists in `the_menu` dictionary.
   - If `opt` is valid, it prints the associated menu item.
   - If not, it warns the user that the option is invalid.

### To Do:
- **TODO 1**: Add options to `the_menu` and define the `print_main_menu()` function.
- **TODO 2**: Implement
Transcribed Image Text:The provided script is a Python program designed for a command-line menu interface. The script will likely appear on an educational website to demonstrate basic control flow and input handling in Python. Below is a transcription and explanation of the code: ```python 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 if 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") ``` ### Detailed Explanation: 1. **Initialization**: - `the_menu` is a dictionary intended to hold menu options. It’s currently empty and requires filling as per instructions. - `opt` is initialized as `None` to store user input. 2. **Main Loop**: - A `while True` loop continuously runs, allowing users to interact with the menu until a specific condition is met to exit. 3. **User Input**: - The program prompts the user to enter an option with `print("::: Enter an option")` followed by `input("> ")` to capture input. 4. **Condition for Quitting**: - A placeholder condition `if opt == ...:` is set up for quitting the program. The developer should replace `...` with conditions like if `opt` is 'Q' or 'q'. 5. **Menu Selection**: - The code checks if the input `opt` exists in `the_menu` dictionary. - If `opt` is valid, it prints the associated menu item. - If not, it warns the user that the option is invalid. ### To Do: - **TODO 1**: Add options to `the_menu` and define the `print_main_menu()` function. - **TODO 2**: Implement
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education