π 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:
-
Create a list of sandwich orders:
The variable
sandwich_orders
is assigned a list containing various sandwich orders, including multiple occurrences of "pastrami". -
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. -
Process each sandwich order:
A
for
loop iterates through thesandwich_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.
-
Remove all occurrences of "pastrami":
A
while
loop is used to remove all occurrences of "pastrami" from thesandwich_orders
list. The loop continues until "pastrami" is no longer in the list. -
Display finished sandwiches:
After processing all orders, a
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.
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:
- ➤ 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