Unit 3 Task 15 - Solution

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Solution - Task 15: Inventory commands

← Back to task


One possible program:

inventory = {"pencil": 20, "eraser": 15}

while True:
    command = input("Command: ").strip()
    if command == "stop":
        break
    if command == "show":
        for product in inventory:
            print(product, inventory[product])
    elif command.startswith("add "):
        parts = command.split()
        product = parts[1]
        qty = int(parts[2])
        inventory[product] = inventory[product] + qty