📘 Python Crash Course – Chapter 7: User Input And while loops – Exercise 7-8 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
📝 Exercise 7-8
Task:
Make a list called sandwich_orders and fill it with the names of various sandwiches . Then make an empty list called finished_sandwiches .
Loop
through the list of sandwich orders and print a message for each order, such
as I made your tuna sandwich.
As each sandwich is made, move it to the list
of finished sandwiches . After all the sandwiches have been made, print a
message listing each sandwich that was made
✅ Solution Code:
sandwich_orders = ['tuna', 'chicken', 'egg', 'ham']
finished_sandwiches = []
for sandwich in sandwich_orders:
print(f"I made your {sandwich} sandwich.")
finished_sandwiches.append(sandwich) # add the sandwich to the finished list
print("-----------------------------------------------------------------")
for sandwich in finished_sandwiches:
print(f"Your {sandwich} sandwich is ready.")
🧠 Code Explanation:
-
Create a list of sandwich orders:
The variable
sandwich_ordersis assigned a list containing the names of various sandwiches to be prepared, such as['tuna', 'chicken', 'egg', 'ham']. -
Create an empty list for finished sandwiches:
The variable
finished_sandwichesis initialized as an empty list. This list will store the sandwiches that have been made. -
Process each sandwich order:
A
forloop iterates through thesandwich_orderslist. For each sandwich:- A message is displayed indicating that the sandwich has been made.
- The sandwich is added to the
finished_sandwicheslist using theappend()method.
-
Display finished sandwiches:
After all the sandwiches have been processed, another
forloop iterates through thefinished_sandwicheslist. For each sandwich in the list, a message is displayed indicating that the sandwich is ready.
🔍 Output:
I made your tuna sandwich.
I made your chicken sandwich.
I made your egg sandwich.
I made your ham sandwich.
-----------------------------------------------------------------
Your tuna sandwich is ready.
Your chicken sandwich is ready.
Your egg sandwich is ready.
Your ham sandwich is ready.
📚 Related Exercises from Chapter 7:
- ➤ Exercise 7-1 | Rental Car
- ➤ Exercise 7-2 | Restaurant Seating
- ➤ Exercise 7-3 | Multiples of Ten
- ➤ Exercise 7-4 | Pizza Toppings
- ➤ Exercise 7-5 | Movie Tickets
- ➤ Exercise 7-6 | Three Exits
- ➤ Exercise 7-7 | Infinity
- ➤ Exercise 7-8 | Deli
- ➤ Exercise 7-9 | No Pastrami
- ➤ Exercise 7-10 | Dream Vacation
🔗 Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”
— Only Python
📌 Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips