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
279 changes: 277 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,281 @@
"# Lab | Functions"
]
},
{
"cell_type": "markdown",
"id": "f3641192",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders with Functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10247dda",
"metadata": {},
"outputs": [],
"source": [
"# Products :\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 1. Define a function named `initialize_inventory` that takes `products` as a parameter. \n",
"# Inside the function, implement the code for initializing the inventory dictionary using a loop and user input:\n",
"\n",
"def initialize_inventory(products):\n",
" \n",
" inventory = {}\n",
" print(\"\\nInventory\")\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the available quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory\n",
"\n",
"# 2. Define a function named `get_customer_orders` that takes no parameters. \n",
"# Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"# The function should return the `customer_orders` set:\n",
"\n",
"def get_customer_orders():\n",
" \n",
" customer_orders = set()\n",
" print(\"\\nCustomer order:\" )\n",
"\n",
" while True:\n",
" product_name = input(\"Enter the name of a product to add to the order: \").strip().lower()\n",
"\n",
" if product_name in products:\n",
" customer_orders.add(product_name)\n",
" print(f\"{product_name} added to the order.\")\n",
" else:\n",
" print(\"Invalid product, this item will be ignored.\")\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another not in (\"yes\", \"y\", \"Yes\", \"Y\", \"YES\"):\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"# 3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders:\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
"\n",
" for ordered_product in customer_orders:\n",
" if inventory.get(ordered_product, 0) > 0:\n",
" inventory[ordered_product] -= 1\n",
" else:\n",
" print(f\"Warning: no inventory left for '{ordered_product}'.\")\n",
"\n",
"# 4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. \n",
"# Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
"# The function should return these values:\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"# 5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. \n",
"# Inside the function, implement the code for printing the order statistics:\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \n",
" total_products_order, percentage_order = order_statistics\n",
"#Tuple unpacking : to assign the values of a tuple to multiple variables in a single line\n",
"\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_order}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_order:.2f}%\")\n",
"\n",
"#6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
"# Inside the function, implement the code for printing the updated inventory:\n",
"def print_updated_inventory(inventory):\n",
" \n",
" print(\"\\nUpdated Inventory\")\n",
" for product in products:\n",
" print(f\"{product}: {inventory[product]}\")\n",
"\n",
"# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"# Hints for functions:\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",
"def final_inventory():\n",
" \n",
" inventory = initialize_inventory(products)\n",
"\n",
" \n",
" customer_orders = get_customer_orders()\n",
"\n",
" \n",
" order_stats = calculate_order_statistics(customer_orders, products)\n",
"\n",
" \n",
" update_inventory(customer_orders, inventory)\n",
"\n",
" \n",
" print_order_statistics(order_stats)\n",
" print_updated_inventory(inventory)\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" final_inventory()\n"
]
},
{
"cell_type": "markdown",
"id": "5f167888",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders with Functions\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b9e16173",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Inventory\n",
"\n",
"Customer order:\n",
"mug added to the order.\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.00%\n",
"\n",
"Updated Inventory\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 5\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"# Products :\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 1. Define a function named `initialize_inventory` that takes `products` as a parameter. \n",
"# Inside the function, implement the code for initializing the inventory dictionary using a loop and user input:\n",
"\n",
"def initialize_inventory(products):\n",
" \n",
" inventory = {}\n",
" print(\"\\nInventory\")\n",
"\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the available quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
" return inventory\n",
"\n",
"# 2. Define a function named `get_customer_orders` that takes no parameters. \n",
"# Inside the function, implement the code for prompting the user to enter the product names using a loop. \n",
"# The function should return the `customer_orders` set:\n",
"\n",
"def get_customer_orders():\n",
" \n",
" customer_orders = set()\n",
" print(\"\\nCustomer order:\" )\n",
"\n",
" while True:\n",
" product_name = input(\"Enter the name of a product to add to the order: \").strip().lower()\n",
"\n",
" if product_name in products:\n",
" customer_orders.add(product_name)\n",
" print(f\"{product_name} added to the order.\")\n",
" else:\n",
" print(\"Invalid product, this item will be ignored.\")\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another not in (\"yes\", \"y\", \"Yes\", \"Y\", \"YES\"):\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"# 3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. \n",
"# Inside the function, implement the code for updating the inventory dictionary based on the customer orders:\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
"\n",
" for ordered_product in customer_orders:\n",
" if inventory.get(ordered_product, 0) > 0:\n",
" inventory[ordered_product] -= 1\n",
" else:\n",
" print(f\"Warning: no inventory left for '{ordered_product}'.\")\n",
"\n",
"# 4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. \n",
"# Inside the function, implement the code for calculating the order statistics (total products ordered, and percentage of unique products ordered). \n",
"# The function should return these values:\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
"\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"# 5. Define a function named `print_order_statistics` that takes `order_statistics` as a parameter. \n",
"# Inside the function, implement the code for printing the order statistics:\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \n",
" total_products_order, percentage_order = order_statistics\n",
"#Tuple unpacking : to assign the values of a tuple to multiple variables in a single line\n",
"\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_order}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_order:.2f}%\")\n",
"\n",
"#6. Define a function named `print_updated_inventory` that takes `inventory` as a parameter. \n",
"# Inside the function, implement the code for printing the updated inventory:\n",
"def print_updated_inventory(inventory):\n",
" \n",
" print(\"\\nUpdated Inventory\")\n",
" for product in products:\n",
" print(f\"{product}: {inventory[product]}\")\n",
"\n",
"# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n",
"# Hints for functions:\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",
"def final_inventory():\n",
" \n",
" inventory = initialize_inventory(products)\n",
"\n",
" \n",
" customer_orders = get_customer_orders()\n",
"\n",
" \n",
" order_stats = calculate_order_statistics(customer_orders, products)\n",
"\n",
" \n",
" update_inventory(customer_orders, inventory)\n",
"\n",
" \n",
" print_order_statistics(order_stats)\n",
" print_updated_inventory(inventory)\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" final_inventory()\n"
]
},
{
"cell_type": "markdown",
"id": "0c581062-8967-4d93-b06e-62833222f930",
Expand Down Expand Up @@ -47,7 +322,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +336,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down