Challenge: Shopping List
Completed
Manage a shopping list. The first line is a number n; then come n commands:
add <item>– add the item to the endremove <item>– remove it if it's in the list
At the end, print the items numbered from 1. If the list is empty print Nothing to buy.
Input
A number n, then n lines of commands.
Output
Lines like 1. milk, or Nothing to buy.
Example
Input
4 add milk add bread add eggs remove bread
Output
1. milk 2. eggs
Editor
Python
Hints
Each command line splits into two words: the action and the item.
Check item in items before removing.
enumerate(items, start=1) numbers the items.
Solution explanation
A loop reads each command and changes the list. An empty list counts as False, so if items: tells us whether anything is left.
Show solution
items = []
n = int(input())
for _ in range(n):
action, item = input().split()
if action == "add":
items.append(item)
elif item in items:
items.remove(item)
if items:
for i, item in enumerate(items, start=1):
print(f"{i}. {item}")
else:
print("Nothing to buy")