diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..1935518 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,375 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Functions" + ] + }, + { + "cell_type": "markdown", + "id": "588191d8-1610-4938-aa11-83fa93557370", + "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": 32, + "id": "c089bf4d-1314-46a0-b2d7-6d1832ee9d34", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 400\n", + "Please input the amount of mugs: 300\n", + "Please input the amount of hats: 200\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 1050\n", + "Final Dictionary: {'t-shirt': 400, 'mug': 300, 'hat': 200, 'book': 100, 'keychain': 50}\n" + ] + } + ], + "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", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + " \n", + " total_items = sum(inventory.values())\n", + " print(f\"The total number of items in stock is: {total_items}\")\n", + " \n", + " return inventory\n", + "\n", + "# Test:\n", + "my_inventory = initialize_inventory(products)\n", + "print(\"Final Dictionary:\", my_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "992ab48e-189e-4e82-82d8-eaa3c3562297", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input product 1 you wish to order: mug\n", + "Please input product 2 you wish to order: book\n", + "Please input product 3 you wish to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Your final order is: {'book', 'mug', 'keychain'}\n" + ] + } + ], + "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. \n", + "# The function should return the customer_orders set.\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " \n", + " for i in range(3):\n", + " order = input(f\"Please input product {i+1} you wish to order: \")\n", + " \n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " \n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders() \n", + "print(\"\\nYour final order is:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "591bcda2-8716-4550-97d3-627d67c2e4ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated t-shirt: 399 left in stock.\n", + "Updated mug: 299 left in stock.\n", + "\n", + "Final Inventory Status: {'t-shirt': 399, 'mug': 299, 'hat': 200, 'books': 100, 'keychains': 50}\n" + ] + } + ], + "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", + " \n", + " for product in customer_orders:\n", + " \n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " print(f\"Updated {product}: {inventory[product]} left in stock.\")\n", + " else:\n", + " print(f\"Warning: {product} is not in the inventory records.\")\n", + " \n", + " return inventory\n", + "\n", + "# Test\n", + "current_inventory = {\"t-shirt\": 400, \"mug\": 300, \"hat\": 200, \"books\": 100, \"keychains\": 50}\n", + "orders = {\"t-shirt\", \"mug\"} \n", + "\n", + "updated_stock = update_inventory(orders, current_inventory)\n", + "\n", + "print(\"\\nFinal Inventory Status:\", updated_stock)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "4b850db7-88ee-410f-b4a2-62747bce1464", + "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).\n", + "# The function should return these values.\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_ordered = len(customer_orders)\n", + " total_products = len(products)\n", + "\n", + " try:\n", + " percentage = (total_ordered / total_products) * 100\n", + " except ZeroDivisionError:\n", + " percentage = 0\n", + " \n", + " return total_ordered, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "5fb0b98e-5c76-466f-9a61-18535fbc39ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-- Order Summary --\n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.00%\n" + ] + } + ], + "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", + "\n", + " total_ordered, percentage = order_statistics\n", + " print(\"-- Order Summary --\")\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " \n", + " print(f\"Percentage of products ordered: {percentage:.2f}%\")\n", + "\n", + "# Test\n", + "my_stats = (2, 40.0) \n", + "print_order_statistics(my_stats)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 202, + "id": "c14ff6f1-cd52-46f0-a477-4715985a7700", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated t-shirt: 399 left in stock.\n", + "Updated mug: 299 left in stock.\n", + "-- Updated inventory --\n", + "Product: t-shirt | Stock: 399\n", + "Product: mug | Stock: 299\n", + "Product: hat | Stock: 200\n", + "Product: books | Stock: 100\n", + "Product: keychains | Stock: 50\n" + ] + } + ], + "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", + "\n", + " print(\"-- Updated inventory --\")\n", + " \n", + " for product, quantity in inventory.items():\n", + " print(f\"Product: {product} | Stock: {quantity}\")\n", + "\n", + "# Test\n", + "current_inventory = {\"t-shirt\": 400, \"mug\": 300, \"hat\": 200, \"books\": 100, \"keychains\": 50}\n", + "orders = {\"t-shirt\", \"mug\"}\n", + "\n", + "updated_stock = update_inventory(orders, current_inventory)\n", + "print_updated_inventory(updated_stock)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 206, + "id": "948cefd8-4e58-47b1-9ba6-dff9745651a4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 400\n", + "Please input the amount of mugs: 300\n", + "Please input the amount of hats: 200\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 1050\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input product 1 you wish to order: mug\n", + "Please input product 2 you wish to order: hat\n", + "Please input product 3 you wish to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated hat: 199 left in stock.\n", + "Updated mug: 299 left in stock.\n", + "Updated keychain: 49 left in stock.\n", + "-- Order Summary --\n", + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00%\n", + "-- Updated inventory --\n", + "Product: t-shirt | Stock: 400\n", + "Product: mug | Stock: 299\n", + "Product: hat | Stock: 199\n", + "Product: book | Stock: 100\n", + "Product: keychain | Stock: 49\n", + "Success! The order has been processed and the inventory is updated.\n" + ] + } + ], + "source": [ + "# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "current_inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, current_inventory)\n", + "\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(current_inventory)\n", + "\n", + "print(\"Success! The order has been processed and the inventory is updated.\")" + ] + } + ], + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..1935518 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "markdown", - "id": "0c581062-8967-4d93-b06e-62833222f930", + "id": "588191d8-1610-4938-aa11-83fa93557370", "metadata": { "tags": [] }, @@ -43,6 +43,312 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "c089bf4d-1314-46a0-b2d7-6d1832ee9d34", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 400\n", + "Please input the amount of mugs: 300\n", + "Please input the amount of hats: 200\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 1050\n", + "Final Dictionary: {'t-shirt': 400, 'mug': 300, 'hat': 200, 'book': 100, 'keychain': 50}\n" + ] + } + ], + "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", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " for product in products:\n", + " amount = int(input(f\"Please input the amount of {product}s: \"))\n", + " inventory[product] = amount \n", + " \n", + " total_items = sum(inventory.values())\n", + " print(f\"The total number of items in stock is: {total_items}\")\n", + " \n", + " return inventory\n", + "\n", + "# Test:\n", + "my_inventory = initialize_inventory(products)\n", + "print(\"Final Dictionary:\", my_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "992ab48e-189e-4e82-82d8-eaa3c3562297", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input product 1 you wish to order: mug\n", + "Please input product 2 you wish to order: book\n", + "Please input product 3 you wish to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Your final order is: {'book', 'mug', 'keychain'}\n" + ] + } + ], + "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. \n", + "# The function should return the customer_orders set.\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " \n", + " for i in range(3):\n", + " order = input(f\"Please input product {i+1} you wish to order: \")\n", + " \n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(\"Sorry, we don't have that!\")\n", + " \n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders() \n", + "print(\"\\nYour final order is:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "591bcda2-8716-4550-97d3-627d67c2e4ef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated t-shirt: 399 left in stock.\n", + "Updated mug: 299 left in stock.\n", + "\n", + "Final Inventory Status: {'t-shirt': 399, 'mug': 299, 'hat': 200, 'books': 100, 'keychains': 50}\n" + ] + } + ], + "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", + " \n", + " for product in customer_orders:\n", + " \n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " print(f\"Updated {product}: {inventory[product]} left in stock.\")\n", + " else:\n", + " print(f\"Warning: {product} is not in the inventory records.\")\n", + " \n", + " return inventory\n", + "\n", + "# Test\n", + "current_inventory = {\"t-shirt\": 400, \"mug\": 300, \"hat\": 200, \"books\": 100, \"keychains\": 50}\n", + "orders = {\"t-shirt\", \"mug\"} \n", + "\n", + "updated_stock = update_inventory(orders, current_inventory)\n", + "\n", + "print(\"\\nFinal Inventory Status:\", updated_stock)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "4b850db7-88ee-410f-b4a2-62747bce1464", + "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).\n", + "# The function should return these values.\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_ordered = len(customer_orders)\n", + " total_products = len(products)\n", + "\n", + " try:\n", + " percentage = (total_ordered / total_products) * 100\n", + " except ZeroDivisionError:\n", + " percentage = 0\n", + " \n", + " return total_ordered, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "5fb0b98e-5c76-466f-9a61-18535fbc39ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-- Order Summary --\n", + "Total products ordered: 2\n", + "Percentage of products ordered: 40.00%\n" + ] + } + ], + "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", + "\n", + " total_ordered, percentage = order_statistics\n", + " print(\"-- Order Summary --\")\n", + " print(f\"Total products ordered: {total_ordered}\")\n", + " \n", + " print(f\"Percentage of products ordered: {percentage:.2f}%\")\n", + "\n", + "# Test\n", + "my_stats = (2, 40.0) \n", + "print_order_statistics(my_stats)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 202, + "id": "c14ff6f1-cd52-46f0-a477-4715985a7700", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated t-shirt: 399 left in stock.\n", + "Updated mug: 299 left in stock.\n", + "-- Updated inventory --\n", + "Product: t-shirt | Stock: 399\n", + "Product: mug | Stock: 299\n", + "Product: hat | Stock: 200\n", + "Product: books | Stock: 100\n", + "Product: keychains | Stock: 50\n" + ] + } + ], + "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", + "\n", + " print(\"-- Updated inventory --\")\n", + " \n", + " for product, quantity in inventory.items():\n", + " print(f\"Product: {product} | Stock: {quantity}\")\n", + "\n", + "# Test\n", + "current_inventory = {\"t-shirt\": 400, \"mug\": 300, \"hat\": 200, \"books\": 100, \"keychains\": 50}\n", + "orders = {\"t-shirt\", \"mug\"}\n", + "\n", + "updated_stock = update_inventory(orders, current_inventory)\n", + "print_updated_inventory(updated_stock)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 206, + "id": "948cefd8-4e58-47b1-9ba6-dff9745651a4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input the amount of t-shirts: 400\n", + "Please input the amount of mugs: 300\n", + "Please input the amount of hats: 200\n", + "Please input the amount of books: 100\n", + "Please input the amount of keychains: 50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total number of items in stock is: 1050\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please input product 1 you wish to order: mug\n", + "Please input product 2 you wish to order: hat\n", + "Please input product 3 you wish to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated hat: 199 left in stock.\n", + "Updated mug: 299 left in stock.\n", + "Updated keychain: 49 left in stock.\n", + "-- Order Summary --\n", + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00%\n", + "-- Updated inventory --\n", + "Product: t-shirt | Stock: 400\n", + "Product: mug | Stock: 299\n", + "Product: hat | Stock: 199\n", + "Product: book | Stock: 100\n", + "Product: keychain | Stock: 49\n", + "Success! The order has been processed and the inventory is updated.\n" + ] + } + ], + "source": [ + "# 7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "current_inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, current_inventory)\n", + "\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(current_inventory)\n", + "\n", + "print(\"Success! The order has been processed and the inventory is updated.\")" + ] } ], "metadata": { @@ -61,7 +367,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,