From a0e9180bf141663dad5a9597b92915d08b7323f1 Mon Sep 17 00:00:00 2001 From: lugon36 Date: Sat, 17 Jan 2026 17:45:45 +0100 Subject: [PATCH] Unit1Day3Lab2 --- lab-python-functions.ipynb | 270 ++++++++++++++++++++++++++++++++++++- 1 file changed, 268 insertions(+), 2 deletions(-) diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..ccb3379 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,11 +43,277 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "id": "ea116f06", + "metadata": {}, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "1b4edc7a", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + " return inventory \n", + "\n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "eae64637", + "metadata": {}, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "19f666d3", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " customer_orders = set()\n", + " continue_entering = \"yes\"\n", + "\n", + " while continue_entering == \"yes\":\n", + " product = input(\"Enter a product to order: \")\n", + " customer_orders.add(product)\n", + "\n", + " continue_entering = input(\"Do you want to add another product? (yes/no): \")\n", + "\n", + " return customer_orders \n" + ] + }, + { + "cell_type": "markdown", + "id": "ca65de03", + "metadata": {}, + "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 inventory dictionary based on the customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "9b21298f", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] = inventory[product]-1\n" + ] + }, + { + "cell_type": "markdown", + "id": "5cf559be", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "185c1522", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " \n", + " total_products_available=len(products)\n", + " total_products_ordered=len(customer_orders)\n", + " percentage_ordered=(total_products_ordered/total_products_available)*100\n", + "\n", + " return total_products_available, percentage_ordered" + ] + }, + { + "cell_type": "markdown", + "id": "f300d0dc", + "metadata": {}, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "eeee4913", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"***Order Statistics: ***\")\n", + " print(f\"Total Products Ordered: {total_products_ordered} \")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered}%\")\n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "56c01852", + "metadata": {}, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "ffc3287c", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51337b8b", + "metadata": {}, + "outputs": [], + "source": [ + "7. Call the functions in the appropriate sequence to execute the program and manage customer orders." + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "21c80e1e", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory=initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "44842ab7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "6a3c22d3", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders=get_customer_orders()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "c3cb64c1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 4, 'hat': 5, 'book': 7, 'keychain': 8}\n" + ] + } + ], + "source": [ + "update_inventory(customer_orders, inventory)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "9e116450", + "metadata": {}, + "outputs": [], + "source": [ + "order_statistics= calculate_order_statistics(customer_orders, products)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "6b200af8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "***Order Statistics: ***\n", + "Total Products Ordered: 5 \n", + "Percentage of Products Ordered: 40.0%\n" + ] + } + ], + "source": [ + "print_order_statistics(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "d10bbcc3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t-shirt: 4\n", + "mug: 4\n", + "hat: 5\n", + "book: 7\n", + "keychain: 8\n" + ] + } + ], + "source": [ + "print_updated_inventory(inventory)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -61,7 +327,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,