From 0a18b41a92f35895d1457e9fb8c5d80e110bd7e8 Mon Sep 17 00:00:00 2001 From: Charul Mathur Date: Mon, 24 Nov 2025 20:23:31 +0100 Subject: [PATCH] Day 2 Lab 2 solved --- .../lab-python-functions-checkpoint.ipynb | 291 ++++++++++++++++++ lab-python-functions.ipynb | 224 +++++++++++++- 2 files changed, 514 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-functions-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb new file mode 100644 index 0000000..58f4859 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-functions-checkpoint.ipynb @@ -0,0 +1,291 @@ +{ + "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": "ccb5a132-84a3-426d-8075-4e7935864c44", + "metadata": {}, + "outputs": [], + "source": [ + "# The product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6a207734-a619-4bb3-8674-305e1525a0c0", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initializing the inventory dictionary using a loop and user input\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " print(\"Enter inventory quantities: \")\n", + " \n", + " for item in products:\n", + " \n", + " while True:\n", + " quantity = int(input(f\"Quantity of {item}: \"))\n", + " \n", + " if quantity < 0:\n", + " print(\"Try again! Quantity cannot be negative\")\n", + " continue\n", + " \n", + " inventory[item] = quantity\n", + " break\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "171d0271-b5f1-4638-b8c4-0a8c30c6ef45", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Function getcustomerorders()\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " \n", + " print(\"Available products: \", products)\n", + "\n", + " while True:\n", + " product = input(\"Enter a product the customer wants to order: \").strip().lower()\n", + "\n", + " while product not in products:\n", + " print(\"Invalid Product. Choose from: \", products)\n", + " product = input(\"Enter a valid product: \").strip().lower()\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another_product != \"yes\":\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "440e8581-3db8-45bc-879f-74d27ab3c979", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 3: Update Inventory\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for item in customer_orders:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1\n", + " else:\n", + " print(f\"Not enough stock for {item}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "1dc177cf-8be6-465d-b570-e40b2adc38be", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 4: Calculate order statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "8895fb20-3805-4107-9bf2-fae63004ef0d", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 5: Print Order Statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Order statistics: \")\n", + " print(\"Total products ordered: \", total_products_ordered)\n", + " print(\"Percentage of products ordered: \", f\"{percentage_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b54b2f7d-50f1-47c1-8830-31a9b327b0e8", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 6: Print Updated Inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\n Updated inventory: \")\n", + " for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "33d74eff-cacc-4a76-9f29-dec67b0da0e4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter inventory quantities: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 10\n", + "Quantity of mug: 15\n", + "Quantity of hat: 20\n", + "Quantity of book: 25\n", + "Quantity of keychain: 30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product the customer wants to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product the customer wants to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product the customer wants to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug', 'hat', 'book'}\n", + "Order statistics: \n", + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00%\n", + "\n", + " Updated inventory: \n", + "t-shirt: 10\n", + "mug: 14\n", + "hat: 19\n", + "book: 24\n", + "keychain: 30\n" + ] + } + ], + "source": [ + "#Step 7: Appropriate Sequence to call all functions\n", + "\n", + "def main():\n", + " inventory = initialize_inventory(products)\n", + "\n", + " customer_orders = get_customer_orders()\n", + "\n", + " order_stats = calculate_order_statistics(customer_orders, products)\n", + " print(\"Customer Orders: \", customer_orders)\n", + " print_order_statistics(order_stats)\n", + "\n", + " update_inventory(customer_orders, inventory)\n", + "\n", + " print_updated_inventory(inventory)\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c119cd3-81b8-4d83-a0fd-73dc0fa3ade6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 44d337b..58f4859 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -43,6 +43,228 @@ "\n", "\n" ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "ccb5a132-84a3-426d-8075-4e7935864c44", + "metadata": {}, + "outputs": [], + "source": [ + "# The product list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6a207734-a619-4bb3-8674-305e1525a0c0", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initializing the inventory dictionary using a loop and user input\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " \n", + " print(\"Enter inventory quantities: \")\n", + " \n", + " for item in products:\n", + " \n", + " while True:\n", + " quantity = int(input(f\"Quantity of {item}: \"))\n", + " \n", + " if quantity < 0:\n", + " print(\"Try again! Quantity cannot be negative\")\n", + " continue\n", + " \n", + " inventory[item] = quantity\n", + " break\n", + " \n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "171d0271-b5f1-4638-b8c4-0a8c30c6ef45", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Function getcustomerorders()\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " \n", + " print(\"Available products: \", products)\n", + "\n", + " while True:\n", + " product = input(\"Enter a product the customer wants to order: \").strip().lower()\n", + "\n", + " while product not in products:\n", + " print(\"Invalid Product. Choose from: \", products)\n", + " product = input(\"Enter a valid product: \").strip().lower()\n", + "\n", + " customer_orders.add(product)\n", + "\n", + " another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + " if another_product != \"yes\":\n", + " break\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "440e8581-3db8-45bc-879f-74d27ab3c979", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 3: Update Inventory\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for item in customer_orders:\n", + " if inventory[item] > 0:\n", + " inventory[item] -= 1\n", + " else:\n", + " print(f\"Not enough stock for {item}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "1dc177cf-8be6-465d-b570-e40b2adc38be", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 4: Calculate order statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "8895fb20-3805-4107-9bf2-fae63004ef0d", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 5: Print Order Statistics\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + " print(\"Order statistics: \")\n", + " print(\"Total products ordered: \", total_products_ordered)\n", + " print(\"Percentage of products ordered: \", f\"{percentage_ordered:.2f}%\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b54b2f7d-50f1-47c1-8830-31a9b327b0e8", + "metadata": {}, + "outputs": [], + "source": [ + "#Step 6: Print Updated Inventory\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\n Updated inventory: \")\n", + " for item, quantity in inventory.items():\n", + " print(f\"{item}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "33d74eff-cacc-4a76-9f29-dec67b0da0e4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter inventory quantities: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirt: 10\n", + "Quantity of mug: 15\n", + "Quantity of hat: 20\n", + "Quantity of book: 25\n", + "Quantity of keychain: 30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product the customer wants to order: mug\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product the customer wants to order: hat\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter a product the customer wants to order: book\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer Orders: {'mug', 'hat', 'book'}\n", + "Order statistics: \n", + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00%\n", + "\n", + " Updated inventory: \n", + "t-shirt: 10\n", + "mug: 14\n", + "hat: 19\n", + "book: 24\n", + "keychain: 30\n" + ] + } + ], + "source": [ + "#Step 7: Appropriate Sequence to call all functions\n", + "\n", + "def main():\n", + " inventory = initialize_inventory(products)\n", + "\n", + " customer_orders = get_customer_orders()\n", + "\n", + " order_stats = calculate_order_statistics(customer_orders, products)\n", + " print(\"Customer Orders: \", customer_orders)\n", + " print_order_statistics(order_stats)\n", + "\n", + " update_inventory(customer_orders, inventory)\n", + "\n", + " print_updated_inventory(inventory)\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c119cd3-81b8-4d83-a0fd-73dc0fa3ade6", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -61,7 +283,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,