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
94 changes: 85 additions & 9 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"metadata": {
"tags": []
},
"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 Down Expand Up @@ -43,11 +41,89 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #list of products\n",
"\n",
"def initialize_inventory(products): #function to initialize inventory\n",
" inventory = {} #empty dictionary to store inventory\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders(): #function to get customer orders\n",
" customer_orders = set() #empty set to store customer orders\n",
" add_more = \"yes\"\n",
" while add_more == \"yes\":\n",
" product = input(\"Enter a product to order: \")\n",
" customer_orders.add(product)\n",
" add_more = input(\"Do you want to add another product? (yes/no): \").lower()\n",
" return customer_orders\n",
"\n",
"\n",
"def update_inventory (customer_orders, inventory): #function to update inventory\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products): #function to calculate order statistics\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_products = (len(customer_orders) / len(products)) * 100 if products else 0\n",
" return total_products_ordered, percentage_unique_products\n",
"\n",
"def print_order_statistics(order_statistics): #function to print order statistics\n",
" total_products_ordered, percentage_unique_products = order_statistics\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_unique_products:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory): #function to print updated inventory\n",
" print(\"Updated Inventory:\")\n",
" for product in inventory:\n",
" print(product + ':', inventory[product]) \n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total products ordered: 3\n",
"Percentage of unique products ordered: 60.00%\n",
"Updated Inventory:\n",
"t-shirt: 1\n",
"mug: 1\n",
"hat: 3\n",
"book: 3\n",
"keychain: 4\n"
]
}
],
"source": [
"#Execute the functions in order\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"
},
Expand All @@ -61,9 +137,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
"nbformat_minor": 4
}