diff --git a/lab_functions_end.ipynb b/lab_functions_end.ipynb new file mode 100644 index 0000000..dd506f9 --- /dev/null +++ b/lab_functions_end.ipynb @@ -0,0 +1,236 @@ +{ + "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": 14, + "id": "eb74f191", + "metadata": {}, + "outputs": [], + "source": [ + "products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "bca24d45", + "metadata": {}, + "outputs": [], + "source": [ + "#1\n", + "def initialize_inventory(products):\n", + " \"\"\"\n", + " Initializes the inventory dict with products names as keys\n", + " and quantities as values.\n", + "\n", + " returns: dict --> initialized inventory\n", + " \"\"\"\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " product_qt = int(input(f'Enter product quantity for {product}:'))\n", + " if product_qt >= 0:\n", + " inventory[product] = product_qt\n", + " break\n", + " else:\n", + " print('quantity must be positive number')\n", + " return inventory\n", + "\n", + "#2\n", + "def get_customer_orders():\n", + " \"\"\"\n", + " Prompt user to input product names for customer orders\n", + " and returns them as a set.\n", + "\n", + " returns: unique orderes product names\n", + " \"\"\"\n", + " customer_orders = set()\n", + " while True:\n", + " order_item = input('Enter product name to order: ').strip()\n", + " if order_item in products:\n", + " customer_orders.add(order_item)\n", + " print(f'{order_item} added to customer orders')\n", + " while True:\n", + " add_order = input('Order another product? (yes/no):').strip()\n", + " if add_order in ['yes', 'no']:\n", + " break\n", + " else:\n", + " print('Invalid input. Enter \"yes\" or \"no\".')\n", + " if add_order == 'no':\n", + " break\n", + " return customer_orders\n", + "\n", + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"\n", + " Updates inventory dict based on customer orders.\n", + " Reduces prodect quantity for each ordered product if available\n", + "\n", + " returns: None\n", + " \"\"\"\n", + " for order_item in customer_orders:\n", + " if order_item in inventory:\n", + " if inventory[order_item] > 0:\n", + " inventory[order_item] -= 1\n", + " print(f\"{order_item}. New quantity: {inventory[order_item]}\")\n", + " else:\n", + " print(f\"'{order_item}' is out of stock.\")\n", + " else:\n", + " print(f\"'{order_item}' not found in inventory.\")\n", + "\n", + "#4\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"\n", + " Calculates statistics based on customer orders\n", + " - Total nr. of unique products ordered\n", + " - Percentage of unique products ordered compared to available products\n", + " \n", + " returns: tuple (total_ordered, percentage_ordered)\n", + " \"\"\"\n", + " total_ordered = len(customer_orders)\n", + " total_available = len(products)\n", + " percentage_ordered = (total_ordered / total_available) * 100\n", + "\n", + " return (total_ordered, percentage_ordered)\n", + "\n", + "#5\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"\n", + " Prints the statistics calculated\n", + " \"\"\"\n", + " total_ordered, percentage_ordered = order_statistics\n", + " print('Order Statistics:')\n", + " print(f'Total Products Ordered: {total_ordered}')\n", + " print(f'Percentage of Products Ordered: {percentage_ordered:.2f}%')\n", + "\n", + "#6\n", + "def print_updated_inventory(inventory):\n", + " if inventory:\n", + " for product, quantity in inventory.items():\n", + " print(f'{product}: {quantity}')\n", + " else:\n", + " print('Inventory is empty.')\n", + " \n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2ee458ee", + "metadata": {}, + "outputs": [], + "source": [ + "#7 --> 1st option\n", + "current_inventory = initialize_inventory(products)\n", + "orders = get_customer_orders()\n", + "update_inventory(orders, current_inventory)\n", + "order_stats = calculate_order_statistics(orders, products)\n", + "print_order_statistics(order_stats)\n", + "print_updated_inventory(current_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "051a7119", + "metadata": {}, + "outputs": [], + "source": [ + "#7 --> 2nd option\n", + "def inventory_management ():\n", + " \"\"\"\n", + " Functions to initiate the program.\n", + " Calls all previous functions in a logical order\n", + " \"\"\"\n", + " products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + " current_inventory = initialize_inventory(products)\n", + " orders = get_customer_orders()\n", + " update_inventory(orders, current_inventory)\n", + " order_stats = calculate_order_statistics(orders, products)\n", + " print_order_statistics(order_stats)\n", + " print_updated_inventory(current_inventory)\n", + "\n", + " print('Process complete.')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ff269b5", + "metadata": {}, + "outputs": [], + "source": [ + "#7 --> 2nd option \n", + "inventory_management()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}