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
236 changes: 236 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
{
"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."
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "95208ece-a063-4b5b-a9f4-d0a6c5d5b204",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['t-shirt', 'mug', 'hat']\n"
]
}
],
"source": [
"print(products)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "4c619853-f67a-4343-bf40-fde0742377b6",
"metadata": {},
"outputs": [],
"source": [
"#1. Pseudocode: Create a function. The function receives a list of products. Create an empty dictionary called inventory. \n",
"#Loop through each product in the list. Ask the user how many units of that product there are. Save the product and its quantity in the dictionary. \n",
"#Return the inventory dictionary.\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" quantity = int(input(\"Stock amount\"))\n",
" inventory[product] = quantity\n",
" \n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "f8bc18fe-46c3-4865-8c9b-110373c8d121",
"metadata": {},
"outputs": [],
"source": [
"#2. Pseudocode: Create a function. The function receives a list of orders. Create a set for the orders. \n",
"#Loop through each product in the list. Ask the user to input the product they want. Save the product in the set. \n",
"#Return the customer_orders set.\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
"\n",
" for i in range(3):\n",
" product_name = input(\"Product name: \")\n",
" customer_orders.add(product_name)\n",
"\n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "0c0a5728-699e-44ca-97ae-0db72e8975f6",
"metadata": {},
"outputs": [],
"source": [
"#3. Pseudocode: Create a function that takes 2 elements as parameters. Update the inventory dictionary based on the customer orders.\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
"\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] = inventory[product] - 1\n",
"\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "92b868d9-0d39-4c2a-94d8-bd167b477dd0",
"metadata": {},
"outputs": [],
"source": [
"#4. Pseudocode: Counting total orders. Transform into set. Count unique orders. Calculate percentage.\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \n",
" total_products_ordered = len(customer_orders)\n",
"\n",
" percentage_ordered = (len(customer_orders) / len(products)) * 100\n",
"\n",
"\n",
" return total_products_ordered, percentage_ordered"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "69b21c92-b99f-45c0-b0f3-a67b7ed0333b",
"metadata": {},
"outputs": [],
"source": [
"#5. Pseudocode: Create a function with a specific parameter. Print order statistics.\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
"\n",
" print(total_products_ordered, percentage_ordered)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "d1efdddc-f57b-4c1d-913c-acc4aa65e68a",
"metadata": {},
"outputs": [],
"source": [
"#6. Pseudocode: Define a function with a specific parameter. Print updated inventory.\n",
"\n",
"def print_updated_inventory(inventory):\n",
"\n",
" print(inventory)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "eb2ad39e-d055-4d90-8835-a703b62c89ad",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Stock amount 20\n",
"Stock amount 40\n",
"Stock amount 50\n",
"Product name: tshirt\n",
"Product name: mug\n",
"Product name: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 100.0\n",
"{'t-shirt': 20, 'mug': 39, 'hat': 49}\n"
]
}
],
"source": [
"#7 Pseudocode: Call the functions to execute the program in the right steps and manage customer orders.\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(order_statistics)\n",
"\n",
"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.14.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading