diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..dd0aa90 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,418 @@ +{ + "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": 1, + "id": "f7e5582b-fbc5-476a-afc9-b697117b33a7", + "metadata": {}, + "outputs": [], + "source": [ + "### 1 ###\n", + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for i in products:\n", + " inventory[i] = int(input(\"Enter the available quantity of the product: \"))\n", + " print(inventory)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "40629556-b48b-47e8-a90e-41a953972b06", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f96071b8-0ba7-4adb-b52a-5e2fc5ab1acf", + "metadata": {}, + "outputs": [], + "source": [ + "### 2 ###" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "4a1def84-d3ce-4952-bd96-ca3329e203a1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " \n", + " customer_orders = set()\n", + " first_order = input(\"Enter the name of the ordered product: \")\n", + " customer_orders.add(first_order)\n", + " answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + " while answer =='Yes':\n", + " order = input(\"Enter the name of the next ordered product: \")\n", + " customer_orders.add(order)\n", + " answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + "\n", + " return customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22410d68-d981-4e21-a087-35382a9f17b9", + "metadata": {}, + "outputs": [], + "source": [ + "### 3 ###" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e0971f3f-dcd7-42d4-81b2-3372c22c9368", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the ordered product: mug\n", + "Do you want to add another product? Answer Yes/No: No\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "cdfcaac0-7f13-401c-a5b8-e489189f86b4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "b84ca9fa-a0ba-4724-834e-6393bb5e182a", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in inventory:\n", + " if product in customer_orders:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f2d70fc6-2341-4e0a-9ef4-f78a3cce7456", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a02858a9-65c0-4e5d-a176-397aaa01047d", + "metadata": {}, + "outputs": [], + "source": [ + "#### 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." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "a50626fe-9ec4-4673-aa9f-09ec59bba3a8", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(products, customer_orders):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_product_ordered = len(customer_orders)/len(products)*100\n", + " return total_products_ordered, percentage_unique_product_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "6d6155f9-7e2b-460e-8335-bd26089fb6cd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 20.0)" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_statistics = calculate_order_statistics(products, customer_orders)\n", + "order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3c1cd67-2e48-4d53-8cd6-f35e3980ad59", + "metadata": {}, + "outputs": [], + "source": [ + "## 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." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "29051cf6-3718-4ffb-894b-4287f110351c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 20.0)\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(order_statistics)\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf36c350-ec63-4738-b6b1-1b3bc9719d79", + "metadata": {}, + "outputs": [], + "source": [ + "# 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." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "4196d1ec-bf49-45d9-b790-63dbaca1d718", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 8, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(inventory)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca76fee6-6137-4959-8a5b-3591c0ec9444", + "metadata": {}, + "outputs": [], + "source": [ + "# 7 # " + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "ce1e32da-29f6-41c7-b9ea-a22937152563", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the ordered product: mug\n", + "Do you want to add another product? Answer Yes/No: Yes\n", + "Enter the name of the next ordered product: book\n", + "Do you want to add another product? Answer Yes/No: No\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 40.0)\n", + "{'t-shirt': 10, 'mug': 9, 'hat': 10, 'book': 9, 'keychain': 10}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(products, customer_orders)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "200059b5-7d76-422b-b953-1c181ecad92f", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a34e692-39be-4891-a40d-7568bad1e64e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eefae9e9-daa8-4295-a977-2b2172b7c3b8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc1e8355-2b4c-466c-a002-ca12eeec8da7", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..dd0aa90 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,13 +43,362 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f7e5582b-fbc5-476a-afc9-b697117b33a7", + "metadata": {}, + "outputs": [], + "source": [ + "### 1 ###\n", + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for i in products:\n", + " inventory[i] = int(input(\"Enter the available quantity of the product: \"))\n", + " print(inventory)\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "40629556-b48b-47e8-a90e-41a953972b06", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f96071b8-0ba7-4adb-b52a-5e2fc5ab1acf", + "metadata": {}, + "outputs": [], + "source": [ + "### 2 ###" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "4a1def84-d3ce-4952-bd96-ca3329e203a1", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " \n", + " customer_orders = set()\n", + " first_order = input(\"Enter the name of the ordered product: \")\n", + " customer_orders.add(first_order)\n", + " answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + " while answer =='Yes':\n", + " order = input(\"Enter the name of the next ordered product: \")\n", + " customer_orders.add(order)\n", + " answer=input(\"Do you want to add another product? Answer Yes/No: \")\n", + "\n", + " return customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22410d68-d981-4e21-a087-35382a9f17b9", + "metadata": {}, + "outputs": [], + "source": [ + "### 3 ###" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e0971f3f-dcd7-42d4-81b2-3372c22c9368", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the ordered product: mug\n", + "Do you want to add another product? Answer Yes/No: No\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "cdfcaac0-7f13-401c-a5b8-e489189f86b4", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "b84ca9fa-a0ba-4724-834e-6393bb5e182a", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in inventory:\n", + " if product in customer_orders:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f2d70fc6-2341-4e0a-9ef4-f78a3cce7456", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a02858a9-65c0-4e5d-a176-397aaa01047d", + "metadata": {}, + "outputs": [], + "source": [ + "#### 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." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "a50626fe-9ec4-4673-aa9f-09ec59bba3a8", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(products, customer_orders):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_product_ordered = len(customer_orders)/len(products)*100\n", + " return total_products_ordered, percentage_unique_product_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "6d6155f9-7e2b-460e-8335-bd26089fb6cd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 20.0)" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "order_statistics = calculate_order_statistics(products, customer_orders)\n", + "order_statistics" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3c1cd67-2e48-4d53-8cd6-f35e3980ad59", + "metadata": {}, + "outputs": [], + "source": [ + "## 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." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "29051cf6-3718-4ffb-894b-4287f110351c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 20.0)\n" + ] + } + ], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(order_statistics)\n", + "\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf36c350-ec63-4738-b6b1-1b3bc9719d79", + "metadata": {}, + "outputs": [], + "source": [ + "# 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." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "4196d1ec-bf49-45d9-b790-63dbaca1d718", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 8, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(inventory)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ca76fee6-6137-4959-8a5b-3591c0ec9444", + "metadata": {}, + "outputs": [], + "source": [ + "# 7 # " + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "ce1e32da-29f6-41c7-b9ea-a22937152563", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n", + "Enter the available quantity of the product: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the ordered product: mug\n", + "Do you want to add another product? Answer Yes/No: Yes\n", + "Enter the name of the next ordered product: book\n", + "Do you want to add another product? Answer Yes/No: No\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 40.0)\n", + "{'t-shirt': 10, 'mug': 9, 'hat': 10, 'book': 9, 'keychain': 10}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders()\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(products, customer_orders)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "200059b5-7d76-422b-b953-1c181ecad92f", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a34e692-39be-4891-a40d-7568bad1e64e", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eefae9e9-daa8-4295-a977-2b2172b7c3b8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc1e8355-2b4c-466c-a002-ca12eeec8da7", + "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 +410,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,