Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 63 additions & 69 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
@@ -1,69 +1,63 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
"In the previous exercise, you improved the code for managing customer orders by using loops and flow control. Now, let's take it a step further and refactor the code by introducing functions.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Define a function named `initialize_inventory` that takes `products` as a parameter. Inside the function, implement the code for initializing the inventory dictionary using a loop and user input.\n",
"\n",
"2. Define a function named `get_customer_orders` that takes no parameters. Inside the function, implement the code for prompting the user to enter the product names using a loop. The function should return the `customer_orders` set.\n",
"\n",
"3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the inventory dictionary based on the customer orders.\n",
"\n",
"4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). The function should return these values.\n",
"\n",
"5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. Inside the function, implement the code for printing the order statistics.\n",
"\n",
"6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. Inside the function, implement the code for printing the updated inventory.\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"\n",
"Hints for functions:\n",
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
def initialize_inventory(products):
inventory = {}
for product in products:
quantity = int(input(f"Enter the quantity for {product}: "))
inventory[product] = quantity
return inventory

def get_customer_orders():
customer_orders = set()
print("\nEnter product names one by one (type 'ok' to finish):")
while True:
order = input("Product name: ").strip()
if order.lower() == 'ok':
break
customer_orders.add(order)
return customer_orders

def update_inventory(customer_orders, inventory):
for order in customer_orders:
if order in inventory and inventory[order] > 0:
inventory[order] -= 1
elif order in inventory:
print(f" {order} is out of stock!")
else:
print(f" {order} is not in the inventory!")
return inventory

def calculate_order_statistics(customer_orders, products):
total_ordered = len(customer_orders)
percentage_unique = (total_ordered / len(products)) * 100
return total_ordered, percentage_unique

def print_order_statistics(order_statistics):
total_ordered, percentage_unique = order_statistics
print("\n Order Statistics:")
print(f"Total products ordered: {total_ordered}")
print(f"Percentage of unique products ordered: {percentage_unique:.2f}%")

def print_updated_inventory(inventory):
print("\n Updated Inventory:")
for product, quantity in inventory.items():
print(f"{product}: {quantity}")

def main():
products = ["apples", "bananas", "oranges", "grapes"]

print("Initialize Inventory ")
inventory = initialize_inventory(products)

print("\n Get Customer Orders ")
customer_orders = get_customer_orders()

print("\n Update Inventory ")
inventory = update_inventory(customer_orders, inventory)

print("\n Calculate Order Statistics ")
order_statistics = calculate_order_statistics(customer_orders, products)

print_order_statistics(order_statistics)
print_updated_inventory(inventory)

if __name__ == "__main__":
main()