Untitled
unknown
plain_text
3 years ago
1.8 kB
14
Indexable
#exe6_Create a function to update the available quantity of an
#existing product, taking as input the product name and the
#new available quantity.
# Create an empty dictionary to represent the warehouse
warehouse = {}
# Define a function to add a new product to the warehouse
def add_product(name, quantity):
# Check if the product already exists in the warehouse
if name in warehouse:
print(f"Product '{name}' already exists in the warehouse.")
else:
# Add the product to the warehouse with its quantity
warehouse[name] = {'quantity': quantity}
print(f"Product '{name}' has been added to the warehouse.")
# Input the products and their quantities
n = int(input("Enter the number of products: "))
for i in range(n):
name = input("Enter the name of the product: ")
quantity = int(input("Enter the available quantity of the product: "))
add_product(name, quantity)
# Define a function to update the available quantity of an existing product
def update_quantity(name, quantity):
if name in warehouse:
# Update the quantity of the existing product
warehouse[name]['quantity'] = quantity
print(f"The available quantity of '{name}' has been updated to {quantity}.")
else:
print(f"Product '{name}' does not exist in the warehouse.")
# Test the update_quantity function by asking the user to input a product name and quantity
name = input("Enter the name of the product to update: ")
quantity = int(input("Enter the new available quantity of the product: "))
update_quantity(name, quantity)
# Print the contents of the warehouse
print("Current contents of the warehouse:")
for product, details in warehouse.items():
print(f"{product}: {details['quantity']}")
Editor is loading...