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
164 changes: 147 additions & 17 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
"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": []
},
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders with Functions\n",
"\n",
Expand All @@ -39,29 +35,163 @@
"\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",
"- Test your functions individually to ensure they work correctly.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1.** `initialize_inventory(products)` — asks for a quantity per product, returns a fresh `inventory` dict. Builds it locally and returns it, instead of relying on a global `inventory` variable."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2.** `get_customer_orders()` — no parameters, same `while True` + \"add another? (yes/no)\" pattern from the flow-control lab. Returns the `customer_orders` set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"Enter the name of a product the customer wants to order: \")\n",
" customer_orders.add(order)\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \")\n",
" if another.lower().strip() != \"yes\":\n",
" break\n",
" return customer_orders"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3.** `update_inventory(customer_orders, inventory)` — subtracts 1 only for products that were actually ordered. Modifies the `inventory` dict it's given (no return needed, since dicts are mutable and the caller's dict is updated in place)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**4.** `calculate_order_statistics(customer_orders, products)` — returns the `(total, percentage)` tuple."
]
},
{
"cell_type": "code",
"execution_count": null,
"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",
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**5.** `print_order_statistics(order_statistics)` — takes the tuple, prints it."
]
},
{
"cell_type": "code",
"execution_count": null,
"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": "markdown",
"metadata": {},
"source": [
"**6.** `print_updated_inventory(inventory)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**7.** Call everything in sequence. `products` is the only thing that still lives at the top level — everything else is passed between functions as arguments/return values, no globals used inside any function body."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n"
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"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"
"version": "3.x"
}
},
"nbformat": 4,
Expand Down