Final Project: Shopping Cart
Put it all together in one small program
You've learned printing, variables, types, strings, math, lists, if/else, and loops. That's genuinely enough to build something real. Let's make a tiny shopping cart program that ties it all together.
What it will do
- Hold a list of items, each with a name and a price.
- Loop through them, printing each one with an f-string.
- Add up the total cost as it goes.
- Use an
ifto give a discount when the total is high.
The program
cart = [
["apple", 1.20],
["bread", 2.50],
["cheese", 4.00],
]
total = 0
for item in cart:
name = item[0]
price = item[1]
print(f"{name}: {price}")
total = total + price
print("-" * 20)
print(f"Total: {total}")
if total >= 5:
print("You earned a 10% discount!")
total = total * 0.9
print(f"New total: {total}")
else:
print("Spend more to earn a discount.")Every piece here is something you already know. Each item is a small list of [name, price], so item[0] is the name and item[1] is the price. The loop prints each item and grows the total. Then a single if decides whether to apply a discount.
The line "-" * 20 is a fun trick: multiplying a string by a number repeats it, giving a line of 20 dashes as a separator.
Tip: This is how real programs grow "" small familiar pieces, stacked together. You don't need new magic; you combine what you have.
Your turn
Run it, then make it yours: add a new item to the cart, change the prices, or change the discount threshold from 5 to something else. Experiment freely "" you can't break anything, and running again always gives a fresh start. Congratulations: you're writing programs.
Press “Run code” to see the result.
Quick check
Q1 In the cart, each item is a list like ["apple", 1.20] . How do you get the price?
Q2 What does "-" * 20 produce?
Want to save your progress? It's free.
Create a free account