diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb new file mode 100644 index 0000000..1f34eb8 --- /dev/null +++ b/lab-python-functions.ipynb @@ -0,0 +1,240 @@ +{ + "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": "1b542a65", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " for product in products:\n", + " quant = int(input(f\"Enter the quantity of {product}: \"))\n", + " inventory[product] = quant" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "93adafa9", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " orders = set()\n", + " while True:\n", + " order = input(\"Enter the name of a product to order: \")\n", + " orders.add(order)\n", + " more = input(\"Do you want to order another product? (yes/no): \")\n", + " if more.lower() != \"yes\":\n", + " break\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "0b4afb63", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " if order in inventory.keys():\n", + " inventory[order] = inventory[order] - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "064c1af8", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = 0\n", + " for order in customer_orders:\n", + " if order in products:\n", + " total_products_ordered = total_products_ordered + 1\n", + " total_unique_products = len(products)\n", + " percentage_unique_products_ordered = (total_products_ordered / total_unique_products) * 100\n", + " return (total_products_ordered, percentage_unique_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "fdcf9b9d", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(f\"Total products ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of unique products ordered: {order_statistics[1]:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "dfd8a76a", + "metadata": {}, + "outputs": [], + "source": [ + "def print_update_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f72d8c8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 10, 'mug': 10, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "64b13a09", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "2ee4411e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 1\n", + "Percentage of unique products ordered: 20.00%\n" + ] + } + ], + "source": [ + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "f359214e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory:\n", + "t-shirt: 10\n", + "mug: 9\n", + "hat: 10\n", + "book: 10\n", + "keychain: 10\n" + ] + } + ], + "source": [ + "update_inventory(customer_orders, inventory)\n", + "print_update_inventory(inventory)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}