diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..363423d 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -1,47 +1,236 @@ { "cells": [ { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "cell_type": "code", + "execution_count": 49, + "id": "7b4be6cf-fbfb-4ec7-b73f-d9285956d910", "metadata": {}, + "outputs": [], "source": [ - "# Lab | Functions" + "products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']" ] }, { - "cell_type": "markdown", - "id": "0c581062-8967-4d93-b06e-62833222f930", - "metadata": { - "tags": [] - }, + "cell_type": "code", + "execution_count": 34, + "id": "a0521e39-dc55-4c85-8e9c-95334a0d3d8c", + "metadata": {}, + "outputs": [], "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", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " count = int(input(f'How many {product}s are there in the warehouse? '))\n", + " inventory[product] = count\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "3276d713-17f1-4948-b422-9450ced7cd0d", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many t-shirts are there in the warehouse? 50\n", + "How many mugs are there in the warehouse? 40\n", + "How many hats are there in the warehouse? 30\n", + "How many books are there in the warehouse? 60\n", + "How many keychains are there in the warehouse? 45\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "342d430b-563b-429c-be6a-faa8ab64120c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventory: {'t-shirt': 50, 'mug': 40, 'hat': 30, 'book': 60, 'keychain': 45}\n" + ] + } + ], + "source": [ + "print(\"Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "8de79e2a-910c-4cb7-b414-291b1d2ee614", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " while True:\n", + " product = input(\"Enter the name of a product you want to order: \").strip().lower()\n", + " customer_orders.add(product)\n", "\n", - "\n" + " more = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if more != 'yes':\n", + " break\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "6acd5ee6-948c-4b5e-ae55-31269b5eac63", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product you want to order: t-shirt\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product you want to order: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the name of a product you want to order: mug\n", + "Do you want to add another product? (yes/no): no\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "1c3764ea-c90a-45d6-8c10-b8d2b9b6ab3a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug', 'book', 't-shirt'}\n" + ] + } + ], + "source": [ + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "f492249a-64fa-4c00-8606-4e0bf8ac3138", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_stats(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_of_products_ordered = (total_ordered / len(products)) * 100\n", + " return total_products_ordered, int(percentage_of_products_ordered)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "beb2e5f0-bc3e-4375-aa2a-7bdb757b1516", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(inventory, customer_orders):\n", + " for item in customer_orders:\n", + " if item in inventory and inventory[item] > 0:\n", + " inventory[item] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "38dcb295-f38d-4eed-8e62-b0f7a815b418", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = update_inventory(inventory, customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "911f8df4-280c-4cb0-8680-83a22516582c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory: {'t-shirt': 49, 'mug': 39, 'hat': 30, 'book': 59, 'keychain': 45}\n" + ] + } + ], + "source": [ + "print(\"Updated Inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "102eed2e-153d-49fc-b206-10fa3226be98", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_stats(total_products_ordered, percentage_of_products_ordered):\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_products_ordered)\n", + " print(\"Percentage of Products Ordered:\", int(percentage_of_products_ordered), \"%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "1aa1fe84-9a0f-49c7-98eb-5328f6b2209c", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "c088e48b-4e8d-486c-9f51-f985595e4490", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60 %\n", + "Updated Inventory:\n", + "t-shirt: 49\n", + "mug: 39\n", + "hat: 30\n", + "book: 59\n", + "keychain: 45\n" + ] + } + ], + "source": [ + "print_order_stats(total_products_ordered, percentage_of_products_ordered)\n", + "print_updated_inventory(inventory)" ] } ], @@ -61,7 +250,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.3" } }, "nbformat": 4,