← Back to Blog

Dict Mini Project — Shopping List

What you're building

The shopping list is back. But this time each item has a quantity and a price.

A dict instead of a list. More data per entry. Total at checkout.

What you need to know first

  • Dicts — creating, reading, modifying
  • in to check if a key exists
  • items() to loop over key-value pairs
  • pop() to remove a key
  • while True with break
  • input(), float(), int()

The brief

Build a program that:

  • Starts with an empty dict
  • Shows a menu: 1 — Add item, 2 — Remove item, 3 — View list, 4 — Checkout, 5 — Quit
  • On add: asks for item name, quantity, and price per unit — stores all three
  • On remove: asks for item name, removes it if it exists
  • On view: prints each item with quantity and price
  • On checkout: prints the full list and the total cost
  • On quit: exits cleanly

Think before you code

  • What does each entry in the dict look like? What's the key, what's the value?
  • How do you store both quantity and price under one key?
  • How do you calculate the total across all items?
  • What do you check before removing an item?

Your starting point

shopping = {}

while True:
    print("\n1 — Add item")
    print("2 — Remove item")
    print("3 — View list")
    print("4 — Checkout")
    print("5 — Quit")
    choice = input("Your choice: ")

    # your code here

Expected output

Your choice: 1
Item name: Bread
Quantity: 2
Price per unit: $1.50
Added.

Your choice: 1
Item name: Milk
Quantity: 1
Price per unit: $0.90
Added.

Your choice: 3

Your list:
- Bread     x2   $3.00
- Milk      x1   $0.90

Your choice: 4

--- Checkout ---
Bread     x2   $3.00
Milk      x1   $0.90
Total:         $3.90

Your choice: 5
Out.

Heads up!

  • Each dict value needs to hold two things — quantity and price. A nested dict works well here
  • Total = sum of quantity × price for each item
  • Check with in before removing — pop() raises KeyError if the key is missing
  • If the item already exists and the user adds it again — your choice: overwrite or update quantity

The solution

shopping = {}

def print_list(d):
    if not d:
        print("Your list is empty.")
    else:
        print("\nYour list:")
        for item, data in d.items():
            subtotal = data["qty"] * data["price"]
            print(f"- {item:<10} x{data['qty']}   ${subtotal:.2f}")

while True:
    print("\n1 — Add item")
    print("2 — Remove item")
    print("3 — View list")
    print("4 — Checkout")
    print("5 — Quit")
    choice = input("Your choice: ")

    if choice == "1":
        name = input("Item name: ").capitalize()
        qty = int(input("Quantity: "))
        price = float(input("Price per unit: $"))
        shopping[name] = {"qty": qty, "price": price}
        print("Added.")

    elif choice == "2":
        name = input("Item to remove: ").capitalize()
        if name in shopping:
            shopping.pop(name)
            print(f"{name} removed.")
        else:
            print(f'"{name}" is not on your list.')

    elif choice == "3":
        print_list(shopping)

    elif choice == "4":
        if not shopping:
            print("Your list is empty.")
        else:
            print("\n--- Checkout ---")
            total = 0
            for item, data in shopping.items():
                subtotal = data["qty"] * data["price"]
                total += subtotal
                print(f"{item:<10} x{data['qty']}   ${subtotal:.2f}")
            print(f"Total:         ${total:.2f}")

    elif choice == "5":
        print("Out.")
        break

    else:
        print("Invalid choice. Enter 1 to 5.")
[ login to bookmark ] // copied! 18 views · 2 min
// resources
Exercise shopping_list_2.py
← prev Dict Mini Project — Tip Calculator next → Dict Mini Project — Pet Shop
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.