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
318 changes: 287 additions & 31 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
@@ -1,53 +1,309 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"cell_type": "code",
"execution_count": 14,
"id": "1eef9dfb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
}
],
"source": [
"# Lab | Functions"
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"print(products)"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
"metadata": {
"tags": []
},
"cell_type": "code",
"execution_count": 12,
"id": "214d37a8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{}\n"
]
}
],
"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",
"inventory = {}\n",
"print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "dbdd893a",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\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",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\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",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82676f4c",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "272c0c67",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set()\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2d09090d",
"metadata": {},
"outputs": [],
"source": [
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e6927b60",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
"7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
" while True:\n",
" product = input(\"Enter a product to order: \")\n",
" customer_orders.add(product)\n",
" add_another = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
"Hints for functions:\n",
" if add_another.lower() != \"yes\":\n",
" break\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",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "996bce40",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug', 'hat', 'book'}\n"
]
}
],
"source": [
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5258135e",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"\n"
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7933d360",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of Products Ordered: {order_statistics[1]}%\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "e6a0dab9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"order_status[0]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "8c632478",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60.0"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"order_status[1]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "269df459",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f68141b0",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "726556fb",
"metadata": {},
"outputs": [],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b1bee755",
"metadata": {},
"outputs": [],
"source": [
"update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "041714c0",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1bd45071",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e52b1644",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 14\n",
"hat: 19\n",
"book: 24\n",
"keychain: 30\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv (3.14.7.final.0)",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +317,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.7"
}
},
"nbformat": 4,
Expand Down