Level 1
0 / 100 XP

Python Loops Challenge!

Here's another exercise for creating a menu system in Python using loops and arrays:

Write a Python program that allows the user to choose from a list of options for a restaurant menu. The program should display the menu options and ask the user to select one. Then, it should display the details of the selected menu item, including its name, description, and price.

Here's an example of how the program should work:

SQL
Welcome to our restaurant! Here is our menu: 1. Pizza - $10 2. Burger - $8 3. Salad - $5 Please select an option: 2 You have selected: Burger Description: Juicy beef patty with lettuce, tomato, and cheese Price: $8

To complete this exercise, you'll need to create an array of menu items, each with its own name, description, and price. Then, you'll need to use a while loop to display the menu options and ask the user to select one. Finally, you'll need to use the user's input to look up the selected menu item in the array and display its details.

Hints...

Hint 1

  • Create an array of dictionaries like so: menu_items = [ { ... }, { ... } ]

Hint 2

  • Use a while loop to keep the menu active until a valid selection is used like so: while True:
  • When the user selects a valid item, use the break command to end the loop

Hint 3

  • Use a for loop to loop through all dictions in the array and output their information inside of the main loop

Solution

Code solution

In this code, we first create an array of menu items, each with its own name, description, and price. Then, we print a welcome message and use a while loop to display the menu options and ask the user to select one.

Inside the while loop, we use a for loop to print the menu options. For each menu item, we print its number, name, and price. Then, we ask the user to select an option by entering the number of the menu item they want.

Next, we use the `try...…