From d75ae06e700220e8ecc970d184a8d7aaeea42ec4 Mon Sep 17 00:00:00 2001 From: sarahsarsarah Date: Fri, 16 Jan 2026 14:30:28 +0100 Subject: [PATCH] lab done --- lab-python-functions.ipynb | 174 ++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 3 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..a1a5cc3 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -25,6 +25,7 @@ "\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", + "\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", @@ -39,15 +40,182 @@ "\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", + "- Test your functions individually to ensure they work correctly." + ] + }, + { + "cell_type": "markdown", + "id": "8c863854", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "93a2665d", + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "products = []\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " inventory[product] = int(input(\"Enter the number of \" + product + \"s: \"))\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "475e6175", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'book', 'mug'}\n" + ] + } + ], + "source": [ + "#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", + "def get_customer_orders():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " order = input(\"Enter the name of a product that a customer wants to order: \").strip()\n", + "\n", + " if order:\n", + " customer_orders.add(order) # Add the product to the set\n", + " else:\n", + " print(\"Invalid input: Order cannot be empty.\")\n", + "\n", + " another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another != 'yes':\n", + " break\n", + "\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(\"Customer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "467b43a7", + "metadata": {}, + "outputs": [], + "source": [ + "#3. Define a function named `update_inventory` that takes `customer_orders` and `inventory` as parameters. Inside the function, implement the code for updating the \n", + "# inventory dictionary based on the customer orders.\n", + "\n", + "def update_inventory(customer_orders,inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(\"No more \" + product + \"s left in inventory or invalid product.\")\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "396159e4", + "metadata": {}, + "outputs": [], + "source": [ + "#4. Define a function named `calculate_order_statistics` that takes `customer_orders` and `products` as parameters. \n", + "# 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", + "def calculate_order_statistics(customer_orders, products):\n", + " # Calculate total number of unique products ordered\n", + " total_products_ordered = len(customer_orders)\n", + " \n", + " # Calculate percentage of unique products ordered\n", + " percentage_products_ordered = (total_products_ordered / len(products)) * 100\n", + " \n", + " return total_products_ordered, percentage_products_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8fce65f2", + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total)\n", + " print(\"Percentage of Unique Products Ordered:\", percentage, \"%\")\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "475fc7ed", + "metadata": {}, + "outputs": [], + "source": [ + "#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", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product in inventory:\n", + " print(product + \"s: \" + str(inventory[product]))\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "84b31b06", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0 %\n", + "Updated Inventory:\n", + "t-shirts: 4\n", + "mugs: 6\n", + "hats: 6\n", + "books: 4\n", + "keychains: 3\n" + ] + } + ], + "source": [ + "#7. Call the functions in the appropriate sequence to execute the program and manage customer orders.\n", + "\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "update_inventory(customer_orders, inventory)\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +229,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,