diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..1e7c4f8 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,342 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "67c29ba6-5fe5-4d4a-8eb2-fc62adbfc9e8", + "metadata": {}, + "source": [ + "# Lab | Functions" + ] + }, + { + "cell_type": "raw", + "id": "3a984506-2423-4320-939b-88a1b048d220", + "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": 2, + "id": "e60a44f1-5b97-4e0c-923e-504790a7bf25", + "metadata": {}, + "outputs": [], + "source": [ + "#1.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " global inventory\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"please input the quantity of {product}\"))\n", + " inventory[product]= quantity\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "27908571-eb2b-46a1-884b-270f1f6d2808", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "please input the quantity of t-shirt 55\n", + "please input the quantity of mug 244\n", + "please input the quantity of hat 25\n", + "please input the quantity of book 78\n", + "please input the quantity of keychain 33\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 55, 'mug': 244, 'hat': 25, 'book': 78, 'keychain': 33}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "46dd1902-7747-46c1-8899-42bd65f02b19", + "metadata": {}, + "outputs": [], + "source": [ + "#2 \n", + "customer_orders = set()\n", + "def get_customer_orders():\n", + " answer=input(\"do you want to add another product? yes or no\")\n", + " while answer==\"yes\":\n", + " order=input(\"product the customer wants to order\")\n", + " answer=input(\"do you want to add another product? yes or no\")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "bdf34740-9b75-42af-929c-3125f7a01026", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "do you want to add another product? yes or no yes\n", + "product the customer wants to order hat\n", + "do you want to add another product? yes or no yes\n", + "product the customer wants to order mug\n", + "do you want to add another product? yes or no no\n" + ] + }, + { + "data": { + "text/plain": [ + "{'hat', 'mug'}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b4d19da9-2802-4722-8d03-18fc92e1a053", + "metadata": {}, + "outputs": [], + "source": [ + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for products in inventory:\n", + " if products in customer_orders:\n", + " inventory[products] = inventory[products]-1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "c1e86ca1-8692-4ffe-89fc-81d0fb1b4cea", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 55, 'mug': 241, 'hat': 22, 'book': 78, 'keychain': 33}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "03b7bdfd-2bbb-4284-b547-528d4e565ff4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 55, 'mug': 242, 'hat': 23, 'book': 78, 'keychain': 33}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b938ea5c-e5cc-40b6-91a2-d6f085bccc16", + "metadata": {}, + "outputs": [], + "source": [ + "#4\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_ordered = total_products_ordered / sum(inventory.values())\n", + " return total_products_ordered, percentage_unique_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c7d7c58d-8dbb-4f69-9d7d-71c2e62fe714", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 0.004662004662004662)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "f5459923-9357-47a0-96cc-abff937a5741", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "480ea4df-bb6c-4c2a-9b1e-357e37954353", + "metadata": {}, + "outputs": [], + "source": [ + "#5\n", + "def print_order_statistics(order_statistics):\n", + " print (calculate_order_statistics(customer_orders, products))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "05fc13d3-a3ec-41c1-8b20-8a141879384b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 0.004662004662004662)\n" + ] + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "8ae3257d-f6d9-45a3-94d1-436a89993395", + "metadata": {}, + "outputs": [], + "source": [ + "#5\n", + "def print_updated_inventory(inventory):\n", + " print(update_inventory(customer_orders, inventory))" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "030b91bd-7fc9-42d5-9bae-85c248e7006c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 55, 'mug': 240, 'hat': 21, 'book': 78, 'keychain': 33}\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b143c5ad-4314-4919-a370-5ed7c3081e3f", + "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..d9d3284 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -1,16 +1,16 @@ { "cells": [ { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "cell_type": "raw", + "id": "67c29ba6-5fe5-4d4a-8eb2-fc62adbfc9e8", "metadata": {}, "source": [ "# Lab | Functions" ] }, { - "cell_type": "markdown", - "id": "0c581062-8967-4d93-b06e-62833222f930", + "cell_type": "raw", + "id": "3a984506-2423-4320-939b-88a1b048d220", "metadata": { "tags": [] }, @@ -43,13 +43,332 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e60a44f1-5b97-4e0c-923e-504790a7bf25", + "metadata": {}, + "outputs": [], + "source": [ + "#1.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " global inventory\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"please input the quantity of {product}\"))\n", + " inventory[product]= quantity\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "id": "141a322d-635d-48cb-a2cb-63e2ed9b3726", + "metadata": {}, + "source": [ + "#1\n", + "\n", + "def initialize_inventory(products):\n", + "inventory = {}\n", + "for product in products:\n", + "quantitiy = int(input(f\"please input the quantity of {product}\"))\n", + "inventory(product) = quantity #adding a key value pair to the dictionary called inventory. It stores the quantity input for the given key\n", + "return inventory\n" + ] + }, + { + "cell_type": "markdown", + "id": "fc51c863-b0d7-47d5-a0b3-1f339443e6cb", + "metadata": {}, + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products) #call for the function and save result in inventory\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "27908571-eb2b-46a1-884b-270f1f6d2808", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "please input the quantity of t-shirt 55\n", + "please input the quantity of mug 244\n", + "please input the quantity of hat 25\n", + "please input the quantity of book 78\n", + "please input the quantity of keychain 33\n" + ] + }, + { + "data": { + "text/plain": [ + "{'t-shirt': 55, 'mug': 244, 'hat': 25, 'book': 78, 'keychain': 33}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "46dd1902-7747-46c1-8899-42bd65f02b19", + "metadata": {}, + "outputs": [], + "source": [ + "#2 \n", + "customer_orders = set()\n", + "def get_customer_orders():\n", + " answer=input(\"do you want to add another product? yes or no\")\n", + " while answer==\"yes\":\n", + " order=input(\"product the customer wants to order\")\n", + " answer=input(\"do you want to add another product? yes or no\")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8bfd43ad-d323-4a17-b63f-764d0e27aedc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "bdf34740-9b75-42af-929c-3125f7a01026", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "do you want to add another product? yes or no yes\n", + "product the customer wants to order hat\n", + "do you want to add another product? yes or no yes\n", + "product the customer wants to order mug\n", + "do you want to add another product? yes or no no\n" + ] + }, + { + "data": { + "text/plain": [ + "{'hat', 'mug'}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b4d19da9-2802-4722-8d03-18fc92e1a053", + "metadata": {}, + "outputs": [], + "source": [ + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for products in inventory:\n", + " if products in customer_orders:\n", + " inventory[products] = inventory[products]-1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "c1e86ca1-8692-4ffe-89fc-81d0fb1b4cea", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 55, 'mug': 241, 'hat': 22, 'book': 78, 'keychain': 33}" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "03b7bdfd-2bbb-4284-b547-528d4e565ff4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 55, 'mug': 242, 'hat': 23, 'book': 78, 'keychain': 33}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inventory" + ] + }, + { + "cell_type": "markdown", + "id": "f8014509-5a31-44c1-b12a-e7f2774043cc", + "metadata": {}, + "source": [ + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + "for product, quantity in customer_orders.items():\n", + "if product in inventrory:\n", + "inventory(product) -= customer_orders[product]\n", + "return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b938ea5c-e5cc-40b6-91a2-d6f085bccc16", + "metadata": {}, + "outputs": [], + "source": [ + "#4\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_ordered = total_products_ordered / sum(inventory.values())\n", + " return total_products_ordered, percentage_unique_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c7d7c58d-8dbb-4f69-9d7d-71c2e62fe714", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 0.004662004662004662)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "f5459923-9357-47a0-96cc-abff937a5741", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "480ea4df-bb6c-4c2a-9b1e-357e37954353", + "metadata": {}, + "outputs": [], + "source": [ + "#5\n", + "def print_order_statistics(order_statistics):\n", + " print (calculate_order_statistics(customer_orders, products))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "05fc13d3-a3ec-41c1-8b20-8a141879384b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 0.004662004662004662)\n" + ] + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "8ae3257d-f6d9-45a3-94d1-436a89993395", + "metadata": {}, + "outputs": [], + "source": [ + "#5\n", + "def print_updated_inventory(inventory):\n", + " print(update_inventory(customer_orders, inventory))" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "030b91bd-7fc9-42d5-9bae-85c248e7006c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 55, 'mug': 240, 'hat': 21, 'book': 78, 'keychain': 33}\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b143c5ad-4314-4919-a370-5ed7c3081e3f", + "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 +380,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,