diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..225c759 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,378 @@ +{ + "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": 212, + "id": "85adffbe-9bab-4740-8da6-66ff062f8199", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(product_names):\n", + " inventory= {}\n", + " for product in product_names:\n", + " quantity = int(input(f\"Enter a quantity for the {product}:\"))\n", + " inventory[product] = quantity #adding a key-value pair to the dictionary called inventory. It stores the quantity input for the given key(product)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 213, + "id": "6467242a-39db-4d2c-8d2f-cc07597bca27", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a quantity for the t-shirt: 45\n", + "Enter a quantity for the mug: 4\n", + "Enter a quantity for the hat: 6\n", + "Enter a quantity for the book: 4\n", + "Enter a quantity for the keychain: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 45, 'mug': 4, 'hat': 6, 'book': 4, 'keychain': 0}\n" + ] + } + ], + "source": [ + "product_names = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory = initialize_inventory(product_names) #call for the function and save result in inventory\n", + "print(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": 214, + "id": "f0cea770-1624-4ab2-9fbe-6d54d12ff8bc", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(product_names):\n", + " customer_orders={}\n", + " for product in product_names:\n", + " customer_order = int(input(f\"Enter the quantity of {product} you want to buy:\"))\n", + " customer_orders[product] = customer_order\n", + " return customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 215, + "id": "0f8bf2ff-add6-4d87-af4f-985719a2ca77", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt you want to buy: 34\n", + "Enter the quantity of mug you want to buy: 0\n", + "Enter the quantity of hat you want to buy: 4\n", + "Enter the quantity of book you want to buy: 7\n", + "Enter the quantity of keychain you want to buy: 3\n" + ] + } + ], + "source": [ + "customer_orders=get_customer_orders(product_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 216, + "id": "e8212937-3966-49c5-8b32-1d5c1edb1cd3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 34, 'mug': 0, 'hat': 4, 'book': 7, 'keychain': 3}" + ] + }, + "execution_count": 216, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 217, + "id": "725e64b2-0b2e-4c84-8611-7d0c656e0ebf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 217, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders['hat']" + ] + }, + { + "cell_type": "code", + "execution_count": 218, + "id": "169e280b-c1cf-425f-8e02-ec29dfcd9dd1", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory): \n", + " for product, quantity in customer_orders.items():\n", + " if product in inventory:\n", + " inventory[product] -= customer_orders[product]\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 219, + "id": "23d056f3-dbcd-493e-98d5-34592e56aa99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 11, 'mug': 4, 'hat': 2, 'book': -3, 'keychain': -3}" + ] + }, + "execution_count": 219, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 220, + "id": "790f744e-ddaf-40b5-ac8a-68f2acc33bb2", + "metadata": {}, + "outputs": [], + "source": [ + "sum_products_ordered=sum(customer_orders.values())\n", + "Percentage_of_products_ordered = round((sum(customer_orders.values())/sum(inventory.values()))*100,2)\n", + "def calculate_order_statistics(customer_orders, products):\n", + " sum_products_ordered=sum(customer_orders.values())\n", + " Percentage_of_products_ordered = round((sum(customer_orders.values())/sum(inventory.values()))*100,2)\n", + " return sum_products_ordered, Percentage_of_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 221, + "id": "9a3e1ba5-ec50-4a81-95e1-96b4dde2ccd9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(48, 436.36)" + ] + }, + "execution_count": 221, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 222, + "id": "1ce871b8-2ef2-4a11-b834-2b64a24904c5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "48" + ] + }, + "execution_count": 222, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 223, + "id": "7ae583d9-5ba3-457c-9fb0-fcdcffa30faa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "436.36" + ] + }, + "execution_count": 223, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Percentage_of_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 224, + "id": "27cc73b1-e2dc-45b8-9f0a-9d19f9c23137", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(sum_products_ordered, Percentage_of_products_ordered):\n", + " print(f\"Total Products Ordered: {sum_products_ordered}\")\n", + " print(f\"Percentage of Inventory Ordered: {Percentage_of_products_ordered}%\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 225, + "id": "e6ac6341-a297-46c7-a687-eff43d71dcae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 48\n", + "Percentage of Inventory Ordered: 436.36%\n" + ] + } + ], + "source": [ + "print_order_statistics(sum_products_ordered, Percentage_of_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 226, + "id": "24cc9db4-6cda-4405-845b-8cff9b8fae2d", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(inventory):\n", + " print(f\"Updated Inventory:{inventory}\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 227, + "id": "2cc96888-71f5-43df-a37a-6bf91af984b0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:{'t-shirt': 11, 'mug': 4, 'hat': 2, 'book': -3, 'keychain': -3}\n" + ] + } + ], + "source": [ + "print_order_statistics(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "f026e89b-96ba-40c4-8855-117500c5eacb", + "metadata": {}, + "source": [ + "#### " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..225c759 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,320 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 212, + "id": "85adffbe-9bab-4740-8da6-66ff062f8199", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(product_names):\n", + " inventory= {}\n", + " for product in product_names:\n", + " quantity = int(input(f\"Enter a quantity for the {product}:\"))\n", + " inventory[product] = quantity #adding a key-value pair to the dictionary called inventory. It stores the quantity input for the given key(product)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 213, + "id": "6467242a-39db-4d2c-8d2f-cc07597bca27", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a quantity for the t-shirt: 45\n", + "Enter a quantity for the mug: 4\n", + "Enter a quantity for the hat: 6\n", + "Enter a quantity for the book: 4\n", + "Enter a quantity for the keychain: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 45, 'mug': 4, 'hat': 6, 'book': 4, 'keychain': 0}\n" + ] + } + ], + "source": [ + "product_names = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "inventory = initialize_inventory(product_names) #call for the function and save result in inventory\n", + "print(inventory) " + ] + }, + { + "cell_type": "code", + "execution_count": 214, + "id": "f0cea770-1624-4ab2-9fbe-6d54d12ff8bc", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(product_names):\n", + " customer_orders={}\n", + " for product in product_names:\n", + " customer_order = int(input(f\"Enter the quantity of {product} you want to buy:\"))\n", + " customer_orders[product] = customer_order\n", + " return customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 215, + "id": "0f8bf2ff-add6-4d87-af4f-985719a2ca77", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirt you want to buy: 34\n", + "Enter the quantity of mug you want to buy: 0\n", + "Enter the quantity of hat you want to buy: 4\n", + "Enter the quantity of book you want to buy: 7\n", + "Enter the quantity of keychain you want to buy: 3\n" + ] + } + ], + "source": [ + "customer_orders=get_customer_orders(product_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 216, + "id": "e8212937-3966-49c5-8b32-1d5c1edb1cd3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 34, 'mug': 0, 'hat': 4, 'book': 7, 'keychain': 3}" + ] + }, + "execution_count": 216, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 217, + "id": "725e64b2-0b2e-4c84-8611-7d0c656e0ebf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 217, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_orders['hat']" + ] + }, + { + "cell_type": "code", + "execution_count": 218, + "id": "169e280b-c1cf-425f-8e02-ec29dfcd9dd1", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory): \n", + " for product, quantity in customer_orders.items():\n", + " if product in inventory:\n", + " inventory[product] -= customer_orders[product]\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 219, + "id": "23d056f3-dbcd-493e-98d5-34592e56aa99", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 11, 'mug': 4, 'hat': 2, 'book': -3, 'keychain': -3}" + ] + }, + "execution_count": 219, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 220, + "id": "790f744e-ddaf-40b5-ac8a-68f2acc33bb2", + "metadata": {}, + "outputs": [], + "source": [ + "sum_products_ordered=sum(customer_orders.values())\n", + "Percentage_of_products_ordered = round((sum(customer_orders.values())/sum(inventory.values()))*100,2)\n", + "def calculate_order_statistics(customer_orders, products):\n", + " sum_products_ordered=sum(customer_orders.values())\n", + " Percentage_of_products_ordered = round((sum(customer_orders.values())/sum(inventory.values()))*100,2)\n", + " return sum_products_ordered, Percentage_of_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 221, + "id": "9a3e1ba5-ec50-4a81-95e1-96b4dde2ccd9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(48, 436.36)" + ] + }, + "execution_count": 221, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 222, + "id": "1ce871b8-2ef2-4a11-b834-2b64a24904c5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "48" + ] + }, + "execution_count": 222, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 223, + "id": "7ae583d9-5ba3-457c-9fb0-fcdcffa30faa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "436.36" + ] + }, + "execution_count": 223, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Percentage_of_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 224, + "id": "27cc73b1-e2dc-45b8-9f0a-9d19f9c23137", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(sum_products_ordered, Percentage_of_products_ordered):\n", + " print(f\"Total Products Ordered: {sum_products_ordered}\")\n", + " print(f\"Percentage of Inventory Ordered: {Percentage_of_products_ordered}%\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 225, + "id": "e6ac6341-a297-46c7-a687-eff43d71dcae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Products Ordered: 48\n", + "Percentage of Inventory Ordered: 436.36%\n" + ] + } + ], + "source": [ + "print_order_statistics(sum_products_ordered, Percentage_of_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 226, + "id": "24cc9db4-6cda-4405-845b-8cff9b8fae2d", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(inventory):\n", + " print(f\"Updated Inventory:{inventory}\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 227, + "id": "2cc96888-71f5-43df-a37a-6bf91af984b0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:{'t-shirt': 11, 'mug': 4, 'hat': 2, 'book': -3, 'keychain': -3}\n" + ] + } + ], + "source": [ + "print_order_statistics(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "f026e89b-96ba-40c4-8855-117500c5eacb", + "metadata": {}, + "source": [ + "#### " + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +370,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,