diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..f9c18a7 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,281 @@ +{ + "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": 40, + "id": "c13676a2-1464-4de6-85f8-f3082c1a9ebb", + "metadata": {}, + "outputs": [], + "source": [ + "#1 function to initialize the inventory\n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"Initializes the inventory dictionary with user-provided quantities.\"\"\"\n", + " inventory = {}\n", + " print(\"Please enter the quantity for each product:\")\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "3cb53531-8f00-4ca1-b0b5-b0b55c1b8899", + "metadata": {}, + "outputs": [], + "source": [ + "#2 function to get customer orders\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"Prompts the user for products to order and returns a set of orders.\"\"\"\n", + " customer_orders = set()\n", + " add_another = 'yes'\n", + " print(\"Please enter the name of products a customer wants to order:\")\n", + " while add_another.lower() == 'yes':\n", + " order_item = input(\"Enter a product to order: \")\n", + " customer_orders.add(order_item)\n", + " add_another = input(\"Do you want to add another product? (yes/no): \")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "f2bebafe-b83e-4252-8e7e-80b81101b664", + "metadata": {}, + "outputs": [], + "source": [ + "#3 function to update the inventory \n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"Updates the inventory by subtracting ordered products.\"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "18c6f40b-2fea-4763-8a27-63a183ab71c5", + "metadata": {}, + "outputs": [], + "source": [ + "#4 function to calculate order statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"Calculates total products and percentage ordered.\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "54c706c7-8791-484f-a928-85a2bdf3c24a", + "metadata": {}, + "outputs": [], + "source": [ + "#5 function to print order statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"Prints the order statistics in a formatted way.\"\"\"\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "f5d5a3b4-4f98-4d12-ad46-b8bb78bb5353", + "metadata": {}, + "outputs": [], + "source": [ + "#6 function to print the updated inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"Prints the updated inventory.\"\"\"\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "466b5fa5-4f70-4c09-949d-542928f6f4f9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter the quantity for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 56\n", + "Enter quantity for mug: 586 \n", + "Enter quantity for hat: 34\n", + "Enter quantity for book: 23\n", + "Enter quantity for keychain: 67\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current Inventory: {'t-shirt': 56, 'mug': 586, 'hat': 34, 'book': 23, 'keychain': 67}\n", + "Please enter the name of products a customer wants to order:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: keychain\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer order: {'mug', 'keychain', 'book'}\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n", + "Updated Inventory:\n", + "t-shirt: 56\n", + "mug: 585\n", + "hat: 34\n", + "book: 22\n", + "keychain: 66\n" + ] + } + ], + "source": [ + "#7 main program execution flow\n", + "\n", + "if __name__ == \"__main__\":\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " # Call the functions in the correct sequence\n", + " current_inventory = initialize_inventory(products)\n", + " print(\"Current Inventory:\", current_inventory)\n", + "\n", + " ordered_products = get_customer_orders()\n", + " print(\"Products in customer order:\", ordered_products)\n", + "\n", + " updated_inventory = update_inventory(ordered_products, current_inventory)\n", + " \n", + " order_stats = calculate_order_statistics(ordered_products, products)\n", + " print_order_statistics(order_stats)\n", + " \n", + " print_updated_inventory(updated_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70a4f146-9df7-400f-a74e-db94b7730736", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d143fce-710c-43e8-a001-d7d5bfb12a20", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b4bb109c-304e-4e1d-a79b-9d58be3ca634", + "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 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..f9c18a7 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,225 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "c13676a2-1464-4de6-85f8-f3082c1a9ebb", + "metadata": {}, + "outputs": [], + "source": [ + "#1 function to initialize the inventory\n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"Initializes the inventory dictionary with user-provided quantities.\"\"\"\n", + " inventory = {}\n", + " print(\"Please enter the quantity for each product:\")\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "3cb53531-8f00-4ca1-b0b5-b0b55c1b8899", + "metadata": {}, + "outputs": [], + "source": [ + "#2 function to get customer orders\n", + "\n", + "def get_customer_orders():\n", + " \"\"\"Prompts the user for products to order and returns a set of orders.\"\"\"\n", + " customer_orders = set()\n", + " add_another = 'yes'\n", + " print(\"Please enter the name of products a customer wants to order:\")\n", + " while add_another.lower() == 'yes':\n", + " order_item = input(\"Enter a product to order: \")\n", + " customer_orders.add(order_item)\n", + " add_another = input(\"Do you want to add another product? (yes/no): \")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "f2bebafe-b83e-4252-8e7e-80b81101b664", + "metadata": {}, + "outputs": [], + "source": [ + "#3 function to update the inventory \n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"Updates the inventory by subtracting ordered products.\"\"\"\n", + " for product in customer_orders:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "18c6f40b-2fea-4763-8a27-63a183ab71c5", + "metadata": {}, + "outputs": [], + "source": [ + "#4 function to calculate order statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"Calculates total products and percentage ordered.\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "54c706c7-8791-484f-a928-85a2bdf3c24a", + "metadata": {}, + "outputs": [], + "source": [ + "#5 function to print order statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"Prints the order statistics in a formatted way.\"\"\"\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "f5d5a3b4-4f98-4d12-ad46-b8bb78bb5353", + "metadata": {}, + "outputs": [], + "source": [ + "#6 function to print the updated inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"Prints the updated inventory.\"\"\"\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "466b5fa5-4f70-4c09-949d-542928f6f4f9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter the quantity for each product:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter quantity for t-shirt: 56\n", + "Enter quantity for mug: 586 \n", + "Enter quantity for hat: 34\n", + "Enter quantity for book: 23\n", + "Enter quantity for keychain: 67\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current Inventory: {'t-shirt': 56, 'mug': 586, 'hat': 34, 'book': 23, 'keychain': 67}\n", + "Please enter the name of products a customer wants to order:\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: keychain\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Products in customer order: {'mug', 'keychain', 'book'}\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0%\n", + "Updated Inventory:\n", + "t-shirt: 56\n", + "mug: 585\n", + "hat: 34\n", + "book: 22\n", + "keychain: 66\n" + ] + } + ], + "source": [ + "#7 main program execution flow\n", + "\n", + "if __name__ == \"__main__\":\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " # Call the functions in the correct sequence\n", + " current_inventory = initialize_inventory(products)\n", + " print(\"Current Inventory:\", current_inventory)\n", + "\n", + " ordered_products = get_customer_orders()\n", + " print(\"Products in customer order:\", ordered_products)\n", + "\n", + " updated_inventory = update_inventory(ordered_products, current_inventory)\n", + " \n", + " order_stats = calculate_order_statistics(ordered_products, products)\n", + " print_order_statistics(order_stats)\n", + " \n", + " print_updated_inventory(updated_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70a4f146-9df7-400f-a74e-db94b7730736", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d143fce-710c-43e8-a001-d7d5bfb12a20", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b4bb109c-304e-4e1d-a79b-9d58be3ca634", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -61,7 +273,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,