- 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 U : Update D : Delete S : Save R : Restore Q : Quit
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 U - Update D - Delete 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]}.")
Hints
- Make sure you do not hard-code the menu options in your functions - the options need to be retrieved from the dictionary provided as a parameter to the function.
enter solution here:
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
can you adjust it so the format looks like this with the equal signs and dashes please
==========================
What would you like to do?
L - List A - Add
U - Update
D - Delete
S - Save the data
R - Restore data from file
Q - Quit this
==========================
can you adjust it so the format looks like this with the equal signs and dashes please
==========================
What would you like to do?
L - List A - Add
U - Update
D - Delete
S - Save the data
R - Restore data from file
Q - Quit this
==========================
- Cost Table The items and costs are stored in two (constant) lists: ITEMS and PRICES. ITEMS = ["Beans", "Rice", "Banana", "Ice", "Tea", "Bread", "Orange", "Sugar"] PRICES = [3.25, 4.31, 6.88, 3.3, 5.25, 4.89, 6.32, 2.25] You should implement the following functions to print a cost table. • make_cost_table(). Called by the main() function, this function will prompt the user for the maximum quantity (between 1 and 9 inclusive), and the orientation of the table (item\gty as horizontal). It will handle the user input error. Once valid values are obtained from the user, this function will call the next function to build the string that contains the formatted cost table, and return it. • build_cost_table(items, prices, max_gty, horizontal). This function will build and return a string that represents the cost table by listing for each item the cost of buying that item for a quantity from 1 to the max_gty. If horizontal is True, each row is for one item and each column is for each quantity,…arrow_forwardExamine the following while loops and determine the value of ires at the end of each of the loops, and the number of times each loop executes. (a) ires = 1; while mod (ires, 15) = 0 iresires + 1; end ires = number of times ires = 3; while ires 100 iresires^2; end ires = number of times (b) (c)arrow_forwardthe language is c++ the bold is the user input , code asks the user what the width and height of the array should be using arrays and loops for this codearrow_forward
- Lab 6: Multiplication Table Write a program that will continually ask the user to enter a positive number from standard input and print out the multiplication number table until user enters an alphabet (non number) to quit. Once user enters 'Q' or anything other than a number, the program will print out “Have a nice day" and then exit the program. After reading the user's input the positive number, the program will print out the multiplication table of the inputted number. For example, if user enters 5, the table should be: Multiplication table for 5 is: 2 3 4 5 4 6 8 10 9 12 15 8 12 16 20 5 10 15 20 25 Your program needs continuously ask the user to enter the a positive number until the user enters Q or q to exit the program. Your program must print out your names and lab info as you did in previous lab similar to the following: Program written by: Ron Sha Course info: Cs-102 Lab 6 - Multiplication Table Date: Wed Oct 14 11:35:18 2020 Activate un 1.2345arrow_forwardThe for construct refers to a loop that processes a list of items. Therefore, it continues to operate as long as there are items to process. Is this a true or false statement?arrow_forwardExercise # 8 – Student Number Password Description of the Problem Write a program that inputs your student number and printout your Biography when the student number is correct. Use a Do While / While loop to repeat the process when an incorrect student number is placed. Sample Output Enter your student no. : 29766512 Name : Lastname, Firstname MI Course : [your course here] Favorite [write something here]: [your favorite 1] Favorite [write something here] : [your favorite 2] Favorite quotation : "quotation here" Enter your student no. : 88888888 "Incorrect Student No.!!" Note : Template will not be available for this one. As the code is very short to finish the exercise.arrow_forward
- String Manipulation Task 1: String and Lists Warm Store any sentence of your choice in a variable called sentence. Print out the variable called sentence and explain what happened: Now type now out the below code under it, and explain what happened: splitsentence = sentence.split() print(splitsentence) Hot Hints: You will need to use... input() A variable Add to the program so that it asks the user to enter a word that can be found in your sentence. Loop through every word in the sentence which has been For loop split. If, else Whilst looping, If the word the user has entered matches a print() word in the sentence, the program should display “match" otherwise it should display "no match"arrow_forwardEven though the EXIT clause can be used in any type of loop, it is considered good form to use the EXIT clause only in basic loops. Answers: Yes Noarrow_forwardLAB ASSIGNMENTS IMPORTANT: you should complete the PDP thinking process for each program. Turn in items: 1) book_list.py: you can adapt your Chapter 4 book_list.py Lab program. The program summarizes costs of a book list. It uses all of our standard mipo_ex features. In this version of the program you must use pylnputPlus functions to perform all the input validation and for the main() loop decision. Adjust your program to allow book prices to include $ and cents. Restrict individual book prices to $100 maxium. • Clearly document, with comments, your use of the pyip functions. This program summarizes a book liat. Enter the number of books that you need: Please enter a whole number: three Enter a number greater than 0: 3 Enter the name of book #1: The Mueller Report Enter cost of The Mueller Report, to the nearest dollar: Please enter a whole number: 18 Enter the name of book #2: Educated: A Memoir Enter cost of Educated: A Memoir, to the nearest dollar: Please enter a whole number: 24…arrow_forward
- PYTHON PROGRAMMING ONLY PLZZ Ask the user to enter numbers: You must have a way for the user to stop entering numbers. An example would be to have the user type q. Store the numbers the user entered into a list Calculate the total Calculate the average Calculate the largest number Display the following information to the user: How many numbers the user entered The total The average The largest number Print each value with a description do not just print numbers to the console. Describe each number as they print.arrow_forwardCassignm222 Assignment 2 features designing a program using pseudocode and then completing the program in Python using a list. M4Lab2 asks you to write a Drawing app in Python and Pygame using a list for the colors. Each lab asks you to write pseudocode that plans the program's logic before you write the program in Python and to turn in three things: 1) the pseudocode, 2) a screenshot of the output, and 3) the Python program. # Step 1: Define a colors list to describe 3 colors.# Hint, use their RGB number values Step 2: define a radius variable for your pen at 5, 10 or 15# Test them to see which radius you prefer and use it # Step 3: write the elif for pressing button 2, similar to button 1 # Step 4: write the statement for button 2's color # Step 5: write the else statement for button 3 # Step 6: choose a color from the list??arrow_forwardPYTHON!!! The credit plan at TidBit Computer Store specifies a 10% down payment and an annual interest rate of 12%. Monthly payments are 5% of the listed purchase price, minus the down payment. Write a program that takes the purchase price as input. The program should display a table, with appropriate headers, of a payment schedule for the lifetime of the loan. Each row of the table should contain the following items: The month number (beginning with 1) The current total balance owed The interest owed for that month The amount of principal owed for that month The payment for that month The balance remaining after payment The amount of interest for a month is equal to balance × rate / 12. The amount of principal for a month is equal to the monthly payment minus the interest owed. An example of the program input and output is shown below: Enter the puchase price: 200 (SEE IMAGE FOR PURCHASE PRICE CHART) Results you should get: Input: 200 Output: Purchase price: 200…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY