diff --git a/lab-python-functions-2.ipynb b/lab-python-functions-2.ipynb new file mode 100644 index 0000000..44d337b --- /dev/null +++ b/lab-python-functions-2.ipynb @@ -0,0 +1,69 @@ +{ + "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" + ] + } + ], + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..a8d8adc 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,192 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "985eb0aa", + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def initialize_inventory(products):\n", + " inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", + " return inventory\n", + "inventory = initialize_inventory(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "eec268b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book'}\n" + ] + } + ], + "source": [ + "#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", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " for i in range(3):\n", + " order = input(\"Enter the name of a product to order: \")\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " return customer_orders\n", + "customer_orders = get_customer_orders()\n", + "result = get_customer_orders()\n", + "print(result)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79dd1f9a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 3, 'mug': 2, 'hat': 1, 'book': -2, 'keychain': -1}\n" + ] + } + ], + "source": [ + "#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", + "def update_inventory(customer_orders, inventory):\n", + " for product in products:\n", + " inventory[product] -= 1\n", + " return inventory\n", + "updated_inventory = update_inventory(customer_orders, inventory)\n", + "result = update_inventory(customer_orders, inventory)\n", + "print(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "f151b2e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 1\n", + "Percentage of unique products ordered: 20.00%\n" + ] + } + ], + "source": [ + "#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", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_unique_ordered\n", + "total_products_ordered, percentage_unique_ordered = calculate_order_statistics(customer_orders, products)\n", + "print(f\"Total products ordered: {total_products_ordered}\")\n", + "print(f\"Percentage of unique products ordered: {percentage_unique_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "3378fefb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 1\n", + "Percentage of unique products ordered: 20.00%\n" + ] + } + ], + "source": [ + "#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", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_unique_ordered = order_statistics\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of unique products ordered: {percentage_unique_ordered:.2f}%\")\n", + "order_statistics = (total_products_ordered, percentage_unique_ordered)\n", + "print_order_statistics(order_statistics)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "eb46b749", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 3\n", + "mug: 2\n", + "hat: 1\n", + "book: -2\n", + "keychain: -1\n" + ] + } + ], + "source": [ + "#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", + "def print_updated_inventory(inventory):\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "print_updated_inventory(updated_inventory)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "1263a517", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 1\n", + "Percentage of unique products ordered: 20.00%\n", + "t-shirt: 4\n", + "mug: 3\n", + "hat: 2\n", + "book: 1\n", + "keychain: 0\n" + ] + } + ], + "source": [ + "#Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "def main():\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders()\n", + " updated_inventory = update_inventory(customer_orders, inventory)\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(updated_inventory)\n", + "if __name__ == \"__main__\":\n", + " main()\n", + " " + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -61,7 +242,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,