Python - 29

Python - 29: Building a Simple Inventory System

Inventory systems track items and quantities. This project combines lists, dictionaries, loops, functions, and conditions.

Your challenge: Add items, display the inventory, update one quantity, and check for low stock.

✦ Python Editor
▶ Output

Store each item

Each inventory item is a dictionary with a "name" and "quantity".

The add_item() function creates the dictionary and adds it to the inventory list.

Update a quantity

Loop through the inventory, find the matching item, and change its quantity:

item["quantity"] = item["quantity"] - 1

Check for low stock

Use a condition such as: if item["quantity"] <= 1:

This lets the program find items that may need to be restocked.

Try it: Add another item with a quantity of 1 and check whether it appears in the low-stock alerts.

Good Python habit: Keep the structure of each dictionary consistent so every inventory item uses the same keys.

👀 Show Example Solution
inventory = []

def add_item(name, quantity):

    item = {
        "name": name,
        "quantity": quantity
    }

    inventory.append(item)

add_item("Potion", 5)
add_item("Sword", 2)
add_item("Shield", 1)

print("Inventory:")

for item in inventory:

    print("Item:")
    print(item["name"])

    print("Quantity:")
    print(item["quantity"])

    print("---")

for item in inventory:

    if item["name"] == "Potion":
        item["quantity"] = item["quantity"] - 1

print("Updated Inventory:")

for item in inventory:

    print(item["name"])
    print(item["quantity"])

for item in inventory:

    if item["quantity"] <= 1:

        print("Low Stock Alert:")
        print(item["name"])

The inventory is stored as a list of dictionaries, and loops make it possible to display, update, and check every item.