Skip to content
Open
Show file tree
Hide file tree
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
251 changes: 251 additions & 0 deletions lab-python-functions-solved.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lab | Functions\n",
"\n",
"## Managing Customer Orders with Functions\n",
"\n",
"This notebook refactors the previous customer-order program into reusable functions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The products used in the previous lab are kept here so that the new functions reproduce the same behavior."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Initialize the inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"Create an inventory dictionary with quantities entered by the user.\"\"\"\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Insert quantity of {product}: \"))\n",
" if quantity < 0:\n",
" print(\"Please enter a non-negative quantity.\")\n",
" continue\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Please enter a whole number.\")\n",
"\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Get customer orders"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" \"\"\"Collect valid, unique customer orders until the user chooses to stop.\"\"\"\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" order = input(\"Write the product you want: \").strip()\n",
"\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Item not found\")\n",
"\n",
" continue_order = input(\"Do you want to continue? yes/no: \").strip().lower()\n",
" if continue_order == \"no\":\n",
" break\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Update the inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"Reduce the inventory by one unit for every ordered product.\"\"\"\n",
" for order in customer_orders:\n",
" if order in inventory:\n",
" inventory[order] -= 1\n",
"\n",
" return inventory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. Calculate order statistics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"Calculate the total and percentage of unique products ordered.\"\"\"\n",
" total_products_ordered = len(customer_orders)\n",
"\n",
" if len(products) == 0:\n",
" percentage_unique_products_ordered = 0.0\n",
" else:\n",
" percentage_unique_products_ordered = (\n",
" total_products_ordered / len(products) * 100\n",
" )\n",
"\n",
" return total_products_ordered, percentage_unique_products_ordered"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. Print the order statistics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" \"\"\"Display the order statistics.\"\"\"\n",
" total_products_ordered, percentage_unique_products_ordered = order_statistics\n",
"\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(\n",
" f\"Percentage of unique products ordered: \"\n",
" f\"{percentage_unique_products_ordered:.2f}%\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. Print the updated inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" \"\"\"Display every product and its remaining quantity.\"\"\"\n",
" print(\"Updated inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Execute the program\n",
"\n",
"Set RUN_INTERACTIVE to True when you want to enter the quantities and orders yourself. It is False by default so the notebook can be run without stopping for input."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"RUN_INTERACTIVE = False\n",
"\n",
"if RUN_INTERACTIVE:\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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example execution\n",
"\n",
"This example checks the complete sequence without requiring manual input."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"example_inventory = {product: 2 for product in products}\n",
"example_orders = {\"mug\", \"book\", \"hat\"}\n",
"\n",
"update_inventory(example_orders, example_inventory)\n",
"example_statistics = calculate_order_statistics(example_orders, products)\n",
"print_order_statistics(example_statistics)\n",
"print_updated_inventory(example_inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
69 changes: 0 additions & 69 deletions lab-python-functions.ipynb

This file was deleted.