"""
� Hard — Inventory Management System
You run a small shop. Build an inventory management system using functions and lists.
� Starting Data
pythoninventory = [
("Rice", 10, 50), # (name, quantity, price)
("Wheat", 5, 40),
("Sugar", 8, 60),
("Oil", 3, 120),
("Salt", 15, 20)
]
� Tasks
Each task must be written as its own function.
display_inventory(inventory) — Print all items with index, name, quantity and price in a cle
add_item(inventory, name, qty, price) — Add a new item. If the item already exists, increase
sell_item(inventory, name, qty) — Reduce quantity after a sale. Warn if stock falls below 5.
total_value(inventory) — Return total stock value — quantity × price summed across all items
search_item(inventory, name) — Search by name (case-insensitive). Print details if found, "N
low_stock(inventory) — Return a list of all items with quantity less than 5
"""
cart = [
("Rice", 10, 50), # (name, quantity, price)
("Wheat", 5, 40),
("Sugar", 8, 60),
("Oil", 3, 120),
("Salt", 15, 20)
]
#1. display_inventory(inventory) — Print all items with index, name, quantity and price in a
print("--------------------------------------------------------------------------------")
def display_inventory(cart):
print("Item Quatity Price ")
for i in cart:
for j in range(3):
print(i[j],end=" ")
print()
display_inventory(cart)
#2. add_item(inventory, name, qty, price) — Add a new item. If the item already exists, incr
print("--------------------------------------------------------------------------------")
def add_item(cart, name, qty, price):
execute = True
1
for i in cart:
if(name == i[0]):
k = (i[0],i[1] + qty, i[2])
[Link](i)
[Link](k)
execute = False
break
if execute :
b = []
[Link](name)
[Link](qty)
[Link](price)
[Link](b)
add_item(cart,"ice",5,8)
display_inventory(cart)
#3. sell_item(inventory, name, qty) — Reduce quantity after a sale. Warn if stock falls belo
print("--------------------------------------------------------------------------------")
def sell_item(cart, name, qty):
for i in cart:
if [Link]() == i[0].lower() :
r = i[1] - qty
if(r==0):
[Link](i)
elif r<0:
print(f"item : {i[0]} ")
print(f"sold : {i[1]} ")
print(f"not available any more : {r} ")
[Link](i)
else:
if r<5:
print("!! Restroking recommended !!")
k = [i[0],r,i[2]]
[Link](i)
[Link](k)
break# Stop the loop once the item is found and processed
sell_item(cart, "Rice",6)
display_inventory(cart)
#4. total_value(inventory) — Return total stock value — quantity × price summed across all i
print("--------------------------------------------------------------------------------")
def total_value(cart):
2
tv = 0
for i in cart:
tv = tv +(i[1]*i[2])
return tv
print("total value of stock : ", total_value(cart))
# 5. search_item(inventory, name) — Search by name (case-insensitive). Print details if foun
print("--------------------------------------------------------------------------------")
def search_item(cart, name):
k = False
for i in cart:
if([Link]() == i[0].lower()):
k=True
print(f"Item: {name} is available :{k}")
search_item(cart,"ice")
#6. low_stock(inventory) — Return a list of all items with quantity less than 5
print("--------------------------------------------------------------------------------")
def low_stock(cart):
k = []
for i in cart:
if(i[1]<5):
[Link](i)
return k
print("Cart item with low stock")
print(low_stock(cart))
--------------------------------------------------------------------------------
Item Quatity Price
Rice 10 50
Wheat 5 40
Sugar 8 60
Oil 3 120
Salt 15 20
--------------------------------------------------------------------------------
Item Quatity Price
Rice 10 50
Wheat 5 40
Sugar 8 60
Oil 3 120
Salt 15 20
ice 5 8
3
--------------------------------------------------------------------------------
!! Restroking recommended !!
Item Quatity Price
Wheat 5 40
Sugar 8 60
Oil 3 120
Salt 15 20
ice 5 8
Rice 4 50
--------------------------------------------------------------------------------
total value of stock : 1580
--------------------------------------------------------------------------------
Item: ice is available :True
--------------------------------------------------------------------------------
Cart item with low stock
[('Oil', 3, 120), ['Rice', 4, 50]]