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
208 changes: 208 additions & 0 deletions .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"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": 5,
"id": "8de87d8a-2a31-4c86-adc1-388ffafbee77",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory (products) :\n",
" inventory = {}\n",
" for product in products :\n",
" quantity = int(input(f\"Add the quantity {product} available in the inventory: \"))\n",
" inventory[product] = quantity\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d0978fe2-b88d-4dcb-bae9-0d749939f662",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" answer = \"yes\"\n",
" \n",
" while answer == \"yes\":\n",
" product = input(\"Which product do you want to add: \")\n",
" customer_orders.add(product)\n",
" answer = input(\"Do you want to add another product (yes/no)? \")\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1ce55dc8-1199-4d0d-b3d8-202e755d66ed",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory (customer_orders, inventory) :\n",
" for product in customer_orders :\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3bb4d60b-c044-4a6a-8c5a-5536089fdc89",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products_available = len(products)\n",
" percentage = (total_products_ordered / total_products_available) * 100\n",
"\n",
" return total_products_ordered, percentage"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "3dc0c6f3-aee9-4c7c-a5d8-04b620b6c162",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered = order_statistics[0]\n",
" percentage = order_statistics[1]\n",
" print(\"Order Statistics:\",\n",
" \"Total Products Ordered:\", total_products_ordered,\n",
" \"Percentage of Products Ordered:\", percentage, \"%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3b1f5a5c-e57c-451d-adb9-005cb05d5ffa",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory) :\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "512d9663-4975-4bd1-bb60-c6915598c758",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add the quantity t-shirt available in the inventory: 5\n",
"Add the quantity mug available in the inventory: 4\n",
"Add the quantity hat available in the inventory: 5\n",
"Add the quantity book available in the inventory: 2\n",
"Add the quantity keychain available in the inventory: 1\n",
"Which product do you want to add: mug\n",
"Do you want to add another product (yes/no)? yes\n",
"Which product do you want to add: hat\n",
"Do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics: Total Products Ordered: 2 Percentage of Products Ordered: 40.0 %\n",
"t-shirt: 5\n",
"mug: 3\n",
"hat: 4\n",
"book: 2\n",
"keychain: 1\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
139 changes: 139 additions & 0 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,145 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8de87d8a-2a31-4c86-adc1-388ffafbee77",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory (products) :\n",
" inventory = {}\n",
" for product in products :\n",
" quantity = int(input(f\"Add the quantity {product} available in the inventory: \"))\n",
" inventory[product] = quantity\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d0978fe2-b88d-4dcb-bae9-0d749939f662",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" answer = \"yes\"\n",
" \n",
" while answer == \"yes\":\n",
" product = input(\"Which product do you want to add: \")\n",
" customer_orders.add(product)\n",
" answer = input(\"Do you want to add another product (yes/no)? \")\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1ce55dc8-1199-4d0d-b3d8-202e755d66ed",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory (customer_orders, inventory) :\n",
" for product in customer_orders :\n",
" inventory[product] -= 1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3bb4d60b-c044-4a6a-8c5a-5536089fdc89",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" total_products_available = len(products)\n",
" percentage = (total_products_ordered / total_products_available) * 100\n",
"\n",
" return total_products_ordered, percentage"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "3dc0c6f3-aee9-4c7c-a5d8-04b620b6c162",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_products_ordered = order_statistics[0]\n",
" percentage = order_statistics[1]\n",
" print(\"Order Statistics:\",\n",
" \"Total Products Ordered:\", total_products_ordered,\n",
" \"Percentage of Products Ordered:\", percentage, \"%\")\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3b1f5a5c-e57c-451d-adb9-005cb05d5ffa",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory) :\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "512d9663-4975-4bd1-bb60-c6915598c758",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Add the quantity t-shirt available in the inventory: 5\n",
"Add the quantity mug available in the inventory: 4\n",
"Add the quantity hat available in the inventory: 5\n",
"Add the quantity book available in the inventory: 2\n",
"Add the quantity keychain available in the inventory: 1\n",
"Which product do you want to add: mug\n",
"Do you want to add another product (yes/no)? yes\n",
"Which product do you want to add: hat\n",
"Do you want to add another product (yes/no)? no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics: Total Products Ordered: 2 Percentage of Products Ordered: 40.0 %\n",
"t-shirt: 5\n",
"mug: 3\n",
"hat: 4\n",
"book: 2\n",
"keychain: 1\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders()\n",
"\n",
"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": {
Expand Down