// Data Structures
Lists Mini Project — Shopping List
What you're building
A shopping list manager that runs in a loop.
The user can add items, remove items, and quit when done.
The list updates and prints after every action.
What you need to know first
- Lists — creating and printing
append()andremove()into check if a value existsforloopsif / elif / elsewhile Truewithbreakinput()
The brief
Build a program that:
- Starts with an empty shopping list
- Shows a menu:
1 — Add item,2 — Remove item,3 — Quit - Prints the current list after every action
- Handles a remove request for an item that isn't on the list
- Exits cleanly when the user picks 3
Think before you code
Before writing anything, answer these:
- What kind of loop keeps the menu running until the user quits?
- How do you add an item to a list?
- What do you check before removing an item?
- How do you print every item in the list?
Your starting point
shopping_list = []
while True:
print("\n1 — Add item")
print("2 — Remove item")
print("3 — Quit")
choice = input("Your choice: ")
# your code here
Expected output
1 — Add item
2 — Remove item
3 — Quit
Your choice: 1
Item to add: Bread
Your list:
- Bread
1 — Add item
2 — Remove item
3 — Quit
Your choice: 1
Item to add: Milk
Your list:
- Bread
- Milk
1 — Add item
2 — Remove item
3 — Quit
Your choice: 2
Item to remove: Bread
Your list:
- Milk
1 — Add item
2 — Remove item
3 — Quit
Your choice: 2
Item to remove: Eggs
"Eggs" is not on your list.
1 — Add item
2 — Remove item
3 — Quit
Your choice: 3
List saved. Out.
Heads up!
- Check with
inbefore callingremove()— it raises aValueErrorif the item isn't there - An empty list is still a list — printing it with a
forloop just prints nothing while Trueruns forever until youbreakout of it- Strip and lowercase the input if you want
"bread"and"Bread"to match — optional for this exercise
The solution
shopping_list = []
def print_list(lst):
if len(lst) == 0:
print("Your list is empty.")
else:
print("\nYour list:")
for item in lst:
print(f"- {item}")
while True:
print("\n1 — Add item")
print("2 — Remove item")
print("3 — Quit")
choice = input("Your choice: ")
if choice == "1":
item = input("Item to add: ")
shopping_list.append(item)
print_list(shopping_list)
elif choice == "2":
item = input("Item to remove: ")
if item in shopping_list:
shopping_list.remove(item)
print_list(shopping_list)
else:
print(f'"{item}" is not on your list.')
elif choice == "3":
print("List saved. Out.")
break
else:
print("Invalid choice. Enter 1, 2, or 3.")
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment