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
262 changes: 262 additions & 0 deletions lab-python-functions (labfinish).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
{
"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": "6d9695e7-641d-473e-9e9e-1a332e3f4fbc",
"metadata": {},
"outputs": [],
"source": [
"FUNCTION initialize_inventory(products):\n",
" CREATE empty inventory dict\n",
" FOR each product in products:\n",
" LOOP until valid quantity entered\n",
" STORE in inventory\n",
" RETURN inventory\n",
"\n",
"FUNCTION get_customer_orders():\n",
" CREATE empty customer_orders set\n",
" ASK how many products the customer wants to order (validated)\n",
" FOR that many times:\n",
" LOOP until valid product name entered\n",
" ADD to customer_orders\n",
" RETURN customer_orders\n",
"\n",
"FUNCTION update_inventory(customer_orders, inventory):\n",
" FOR each product in customer_orders:\n",
" SUBTRACT 1 from inventory[product]\n",
" (no return needed — dictionary is modified in place)\n",
"\n",
"FUNCTION calculate_order_statistics(customer_orders, products):\n",
" total = number of items in customer_orders\n",
" percentage = (total / number of items in products) * 100\n",
" RETURN (total, percentage)\n",
"\n",
"FUNCTION print_order_statistics(order_statistics):\n",
" UNPACK order_statistics into total and percentage\n",
" PRINT them in the required format\n",
"\n",
"FUNCTION print_updated_inventory(inventory):\n",
" FOR each product, quantity in inventory:\n",
" PRINT product and quantity\n",
"\n",
"MAIN PROGRAM:\n",
" CALL initialize_inventory\n",
" CALL get_customer_orders\n",
" CALL update_inventory\n",
" CALL calculate_order_statistics\n",
" CALL print_order_statistics\n",
" CALL print_updated_inventory"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "99b2067d-e5f1-48ec-a617-3f35084518ba",
"metadata": {},
"outputs": [],
"source": [
"#Remember our products are: \n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#Our inventory is:\n",
"inventory = {\"t-shirt\":10, \"mug\":12, \"hat\":2, \"book\":3, \"keychain\":35}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "caa3460b-2e3c-431b-bf54-04d34a03e382",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity of t-shirt: 1\n",
"Enter quantity of mug: 3\n",
"Enter quantity of hat: 4\n",
"Enter quantity of book: 1\n",
"Enter quantity of keychain: 0\n",
"Enter a product the customer wants to order: mug\n",
"Enter a product the customer wants to order: tshirt\n",
"Enter a product the customer wants to order: keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0 %\n"
]
},
{
"ename": "KeyError",
"evalue": "'tshirt'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mKeyError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 60\u001b[39m\n\u001b[32m 56\u001b[39m \n\u001b[32m 57\u001b[39m order_statistics = calculate_order_statistics(customer_orders, products)\n\u001b[32m 58\u001b[39m print_order_statistics(order_statistics)\n\u001b[32m 59\u001b[39m \n\u001b[32m---> \u001b[39m\u001b[32m60\u001b[39m update_inventory(customer_orders, inventory)\n\u001b[32m 61\u001b[39m print_updated_inventory(inventory)\n",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 27\u001b[39m, in \u001b[36mupdate_inventory\u001b[39m\u001b[34m(customer_orders, inventory)\u001b[39m\n\u001b[32m 25\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m update_inventory(customer_orders, inventory):\n\u001b[32m 26\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;28;01min\u001b[39;00m customer_orders:\n\u001b[32m---> \u001b[39m\u001b[32m27\u001b[39m inventory[product] = inventory[product] - \u001b[32m1\u001b[39m\n",
"\u001b[31mKeyError\u001b[39m: 'tshirt'"
]
}
],
"source": [
"# List of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"# 1. Function to set up inventory\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = input(\"Enter quantity of \" + product + \": \")\n",
" quantity = int(quantity)\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"\n",
"# 2. Function to get customer orders\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" for i in range(3):\n",
" order = input(\"Enter a product the customer wants to order: \")\n",
" customer_orders.add(order)\n",
" return customer_orders\n",
"\n",
"\n",
"# 3. Function to update inventory\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" inventory[product] = inventory[product] - 1\n",
"\n",
"\n",
"# 4. Function to calculate statistics\n",
"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\n",
"\n",
"\n",
"# 5. Function to print statistics\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered = order_statistics[0]\n",
" percentage_ordered = order_statistics[1]\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total_products_ordered)\n",
" print(\"Percentage of Products Ordered:\", percentage_ordered, \"%\")\n",
"\n",
"\n",
"# 6. Function to print inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product in inventory:\n",
" print(product, \":\", inventory[product])\n",
"\n",
"\n",
"# 7. Run the program\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"\n",
"update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e24b100e-7337-40c8-b3b2-4059d037d7b1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "73ceced7-f156-4178-a7cd-e5e715513c7f",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "129b6294-2908-4459-b160-6c244bf587e5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.14.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}