π 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_orders
is 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_sandwiches
is initialized as an empty list. This list will store the sandwiches that have been made. -
Process each sandwich order:
A
for
loop iterates through thesandwich_orders
list. For each sandwich:- A message is displayed indicating that the sandwich has been made.
- The sandwich is added to the
finished_sandwiches
list using theappend()
method.
-
Display finished sandwiches:
After all the sandwiches have been processed, another
for
loop iterates through thefinished_sandwiches
list. 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
πTrending Topics
π 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
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment