Exercise 7-8 Solution Python Crash Course Chapter 7 : User Input And While Loops

 

📘 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:

  1. 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'].

  2. 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.

  3. Process each sandwich order:

    A for loop iterates through the sandwich_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 the append() method.
  4. Display finished sandwiches:

    After all the sandwiches have been processed, another for loop iterates through the finished_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:


🔗 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

Previous Post Next Post

Contact Form