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
275 changes: 275 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
{
"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": 29,
"id": "2e7a8f61-bdd9-4152-a1a3-e31e7881b46f",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"# Define a list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {\"t-shirt\": 40, \n",
" \"mug\": 25, \n",
" \"hat\" : 30, \n",
" \"book\":12, \n",
" \"keychain\":6\n",
"}\n",
"\n",
"customer_orders = { \"mug\", \"hat\", \"book\"}\n",
"def initialize_inventory(products):\n",
" # Create an empty dictionary\n",
" for p in products:\n",
" product_quantity = int(input(f\"Enter the quantity of {p} available in the inventory: \"))\n",
" inventory [p] = product_quantity\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "b670061c-1f43-432c-a94d-8c19792d8fe7",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"# Define a list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" # Create an empty dictionary\n",
" inventory = {}\n",
" for p in products:\n",
" product_quantity = int(input(f\"Enter the quantity of {p} available in the inventory: \"))\n",
" inventory [p] = product_quantity\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a84e282-a99a-43c4-bca4-80ef64e92ba0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 19,
"id": "aa47c73e-1eeb-404b-b247-8dc8459e4a64",
"metadata": {},
"outputs": [],
"source": [
"# 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. The function should return the customer_orders set.\n",
"\n",
"def get_customer_orders(): \n",
" customer_orders = set()\n",
" add_product = \"yes\"\n",
" while add_product.lower() == \"yes\":\n",
" product_name = input(f\"Enter the name of a product to order: \")\n",
" # Add the product name to the \"customer_orders\" set.\n",
" customer_orders.add(product_name)\n",
" # Ask the user if they want to add another product (yes/no).\n",
" add_product = input(f\"Do you want to add another product ? (yes/no) :\")\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "a009adc0-f892-4280-a453-2112a8809565",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" for p in customer_orders:\n",
" if p in inventory:\n",
" inventory[p] -=1\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "eb2463f3-02f8-42d5-9949-775e6175f11f",
"metadata": {},
"outputs": [],
"source": [
"# 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). The function should return these values.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \n",
" # 1. Total products ordered\n",
" total_product_ordered = len(customer_orders)\n",
" \n",
" # 2. Unique products ordered\n",
" unique_product_ordered = len(set(customer_orders))\n",
"\n",
" # 3. Total available products\n",
" total_products = len(products)\n",
"\n",
" # 4. Percentage of unique products ordered\n",
" if total_products >0:\n",
" percentage_unique_p_ordered = (unique_product_ordered / total_products)* 100\n",
" else:\n",
" percentage_unique_p_ordered = 0\n",
"\n",
" return total_product_ordered, percentage_unique_p_ordered\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "6e99cb43-c9b4-4f93-816d-2b08ec3e7041",
"metadata": {},
"outputs": [],
"source": [
"# 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",
" total_product_ordered, percentage_unique_p_ordered = order_statistics\n",
" print(f\"Total products ordered: {total_product_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_unique_p_ordered:.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "bc0b6792-33df-4545-afb0-0db0b70dd70c",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"\n",
"def print_updated_inventory (inventory):\n",
" print(f\"Updated inventory:\")\n",
" for p, product_quantity in inventory.items():\n",
" print(f\"{p}: {product_quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d80111c2-0149-40fc-bb76-6125f84d5a8c",
"metadata": {},
"outputs": [],
"source": [
"# 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",
"# list of available products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"#start the inventory\n",
"inventory = initialize_inventory(products)\n",
"\n",
"#collect the customer's orders\n",
"customer_orders = get_customer_orders()\n",
"\n",
"# Update the inventory\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"# calculate statistics\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"# display the statistics\n",
"print_order_statistics(order_statistics)\n",
"\n",
"# display updated inventory\n",
"print_updated_inventory(inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1797ed0-d4b3-40a1-ade4-58b6959a44a5",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading