diff --git a/lab_python_functions.ipynb b/lab_python_functions.ipynb new file mode 100644 index 0000000..8332c8d --- /dev/null +++ b/lab_python_functions.ipynb @@ -0,0 +1,221 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": { + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e" + }, + "source": [ + "# Lab | Functions" + ] + }, + { + "cell_type": "markdown", + "id": "0c581062-8967-4d93-b06e-62833222f930", + "metadata": { + "tags": [], + "id": "0c581062-8967-4d93-b06e-62833222f930" + }, + "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", + "source": [], + "metadata": { + "id": "QqC1S3gwDHsj" + }, + "id": "QqC1S3gwDHsj", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "I09czKdmLkm1" + }, + "id": "I09czKdmLkm1", + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# List of products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "\n", + "# 1. Initialize inventory\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + " return inventory\n", + "\n", + "\n", + "# 2. Get customer orders\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " product = input(\"Enter the product you want to order: \")\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + " if another.lower() == \"no\":\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "# 3. Update inventory\n", + "def update_inventory(customer_orders, inventory):\n", + "\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + " return inventory\n", + "\n", + "\n", + "# 4. Calculate order statistics\n", + "def calculate_order_statistics(customer_orders, products):\n", + "\n", + " total_products_ordered = len(customer_orders)\n", + "\n", + " percentage_ordered = (\n", + " total_products_ordered / len(products)\n", + " ) * 100\n", + "\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "\n", + "# 5. Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + "\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", + " print(\"Total products ordered:\", total_products_ordered)\n", + " print(\"Percentage of unique products ordered:\", percentage_ordered, \"%\")\n", + "\n", + "\n", + "# 6. Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + "\n", + " print(\"Updated inventory:\")\n", + "\n", + " for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)\n", + "\n", + "\n", + "# 7. Call the functions in sequence\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(\n", + " customer_orders,\n", + " products\n", + ")\n", + "\n", + "print_order_statistics(order_statistics)\n", + "\n", + "print_updated_inventory(inventory)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Whtml7mNLkeU", + "outputId": "9d4d9e84-e29a-4b3b-860b-83c7820eeef7" + }, + "id": "Whtml7mNLkeU", + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the quantity of t-shirt: 10\n", + "Enter the quantity of mug: 20\n", + "Enter the quantity of hat: 30\n", + "Enter the quantity of book: 40\n", + "Enter the quantity of keychain: 50\n", + "Enter the product you want to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the product you want to order: hat\n", + "Do you want to add another product? (yes/no): no\n", + "Total products ordered: 2\n", + "Percentage of unique products ordered: 40.0 %\n", + "Updated inventory:\n", + "t-shirt : 10\n", + "mug : 19\n", + "hat : 29\n", + "book : 40\n", + "keychain : 50\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" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file