Back to: Python Programming
0
Practise exercise to reinforce the use of the collections “Lists, Sets and Tuples”. We’ll be using two lists for this exercise.
# Shopping Cart
foods = []
prices = []
total = 0
while True:
food = input("Enter a food to buy (q to quit): ")
if food.lower() =="q":
break
else:
price = float(input(f"Enter the price of {food}: $"))
foods.append(food)
prices.append(price)
print("---------Your Cart---------")
for food in foods:
print(food, end=" ")
for price in prices:
total += price
print()
print(f"The total cart value is: ${total:.2f}")
See if you can break this code. Add some extra validation if you need to.