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
307 changes: 307 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
{
"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": 71,
"id": "02e192d1-9f34-47ca-9ba2-7789aba35896",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named initialize_inventory that takes products as a parameter\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"def initialize_inventory(products):\n",
" for product in products:\n",
" quantity = int(input(\"Please enter the quantity of \" + product))\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "57f25918-7a50-4d23-a396-2813a85fe23d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Please enter the quantity of t-shirt 18\n",
"Please enter the quantity of mug 5\n",
"Please enter the quantity of hat 24\n",
"Please enter the quantity of book 12\n",
"Please enter the quantity of keychain 52\n"
]
},
{
"data": {
"text/plain": [
"{'t-shirt': 18, 'mug': 5, 'hat': 24, 'book': 12, 'keychain': 52}"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "918bc3bb-a3c9-4c38-b6b8-82827b153d2d",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named get_customer_orders\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" product = input('Enter the name of a product you want to order: ')\n",
" customer_orders.add(product) # Add valid product to orders\n",
" new_order = input('Do you want to add another product? (Y/N): ')\n",
" if new_order.upper() != 'Y':\n",
" break # Stop the loop if answer is not yes\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "f16c1733-7f71-4375-a0fa-b7feab5d43d9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product you want to order: mug\n",
"Do you want to add another product? (Y/N): Y\n",
"Enter the name of a product you want to order: book\n",
"Do you want to add another product? (Y/N): Y\n",
"Enter the name of a product you want to order: hat\n",
"Do you want to add another product? (Y/N): N\n"
]
},
{
"data": {
"text/plain": [
"{'book', 'hat', 'mug'}"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "30e7468f-e442-4384-99a4-7d285ab1e366",
"metadata": {},
"outputs": [],
"source": [
"# inventory = {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n",
"# customer_orders = {'book', 'hat', 'mug'}\n",
"# Define a function named update_inventory\n",
"def update_inventory(customer_orders,inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product]-=1 #same name \"product\" among set and dictionary\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "9a6e0292-787a-42c1-8de5-d706de19f9c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 18, 'mug': 4, 'hat': 23, 'book': 11, 'keychain': 52}"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"update_inventory(customer_orders,inventory)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "505c33d3-4632-43e6-b02b-cda19724955d",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named calculate_order_statistics\n",
"# customer_orders = {'book', 'hat', 'mug'}\n",
"# products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"def calculate_order_statistics(customer_orders, products):\n",
" Total_Products_Ordered = len(customer_orders)\n",
" Percentage_of_Products_Ordered = len(customer_orders) / len(products) *100\n",
" order_statistics = (Total_Products_Ordered,Percentage_of_Products_Ordered)\n",
" return order_statistics"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "212a06fd-0a61-4dac-a427-a8c20b1913d5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 60.0)"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "aebb8e55-5628-49f0-8446-cd2476f65172",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named print_order_statistics\n",
"# order_statistics = (3, 60.0)\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"Total Products Ordered: {order_statistics[0]}, Percentage of Products Ordered: {order_statistics[1]} % \")"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "a1b82586-4794-4eb7-bfdd-4236b4f01281",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Products Ordered: 3, Percentage of Products Ordered: 60.0 % \n"
]
}
],
"source": [
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "c59e225a-9e9b-4dbe-8a24-b4ef563f8f35",
"metadata": {},
"outputs": [],
"source": [
"# Define a function named print_updated_inventory\n",
"def print_updated_inventory(inventory):\n",
" print(f\"inventory has been updated! {inventory}\")"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "e18a171f-2b6e-4bfb-9cd1-d84a96bbaf44",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"inventory has been updated! {'t-shirt': 18, 'mug': 4, 'hat': 23, 'book': 11, 'keychain': 52}\n"
]
}
],
"source": [
"print_updated_inventory(inventory)"
]
}
],
"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.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading