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
186 changes: 186 additions & 0 deletions lab-python-functions-solved.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4ec232e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 1. Define initialize_inventory\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter initial quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4eb3d05",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 2. Define get_customer_orders\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Enter a product name to order (or type 'done' to finish): \").strip()\n",
" if order.lower() == 'done':\n",
" break\n",
" customer_orders.add(order)\n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76f84661",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 3. Define update_inventory\n",
"def update_inventory(customer_orders, inventory):\n",
" for order in customer_orders:\n",
" if order in inventory and inventory[order] > 0:\n",
" inventory[order] -= 1\n",
" else:\n",
" print(f\"Product '{order}' is out of stock or not found.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64c2c274",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 4. Define calculate_order_statistics\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" unique_products_ordered = len([p for p in customer_orders if p in products])\n",
" percentage_unique = (unique_products_ordered / len(products)) * 100 if products else 0\n",
" return total_products_ordered, percentage_unique\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d14de91e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 5. Define print_order_statistics\n",
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" print(f\"Total products ordered: {total}\")\n",
" print(f\"Percentage of unique products ordered: {percentage:.2f}%\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3ba48f3",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 6. Define print_updated_inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2eb27461",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 7. Main execution flow\n",
"products = [\"apple\", \"banana\", \"orange\", \"grape\", \"mango\"]\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"order_stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_stats)\n",
"print_updated_inventory(inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}