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

 

πŸ“˜ Python Crash Course – Chapter 7: User Input And while loops – Exercise 7-9 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


πŸ“ Exercise 7-9

Task:
Using the list sandwich_orders from Exercise 7-8, make sure the sandwich 'pastrami' appears in the list at least three times . Add code near the beginning of your program to print a message saying the deli has run out of pastrami, and then use a while loop to remove all occurrences of 'pastrami' from sandwich_orders . Make sure no pastrami sandwiches end up in finished_sandwiches .

✅ Solution Code:


sandwich_orders = ['tuna', 'chicken','pastrami', 'egg', 'ham', 'pastrami', 'pastrami']
finished_sandwiches = []
for sandwich in sandwich_orders:
    if sandwich == "pastrami":
        print("deli has run out of pastrami")
        
    else:
       print(f"I made your {sandwich} sandwich.")
       finished_sandwiches.append(sandwich) # add the sandwich to the finished list


while "pastrami" in sandwich_orders:
    sandwich_orders.remove("pastrami") # remove all occurrences of pastrami from sandwich_orders
   

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 various sandwich orders, including multiple occurrences of "pastrami".

  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 successfully made.

  3. Process each sandwich order:

    A for loop iterates through the sandwich_orders list. For each sandwich:

    • If the sandwich is "pastrami", a message is displayed indicating that the deli has run out of pastrami.
    • For all other sandwiches, a message is displayed indicating that the sandwich has been made, and the sandwich is added to the finished_sandwiches list.
  4. Remove all occurrences of "pastrami":

    A while loop is used to remove all occurrences of "pastrami" from the sandwich_orders list. The loop continues until "pastrami" is no longer in the list.

  5. Display finished sandwiches:

    After processing all orders, a 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.
deli has run out of pastrami
I made your egg sandwich.
I made your ham sandwich.
deli has run out of pastrami
deli has run out of pastrami
-----------------------------------------------------------------
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

“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