diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..1c5920d0 Binary files /dev/null and b/.DS_Store differ diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 5b3ce9e0..c2baaf82 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -50,11 +50,99 @@ "\n", "Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tshirt: 10\n", + "mug: 10\n", + "hat: 10\n", + "book: 10\n", + "keychain: 10\n", + "customer orders: {'hat', 'book', 'mug'}\n", + "3\n", + "60.0 %\n", + "6.0 %\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 6.0 %\n", + "Updated Inventory:\n", + "tshirt: 9\n", + "mug: 9\n", + "hat: 9\n", + "book: 9\n", + "keychain: 9\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "inventory[\"tshirt\"] = int(input(\"Enter quantity of tshirt: \"))\n", + "inventory[\"mug\"] = int(input(\"Enter quantity of mug: \"))\n", + "inventory[\"hat\"] = int(input(\"Enter quantity of hat: \"))\n", + "inventory[\"book\"] = int(input(\"Enter quantity of book: \"))\n", + "inventory[\"keychain\"] = int(input(\"Enter quantity of keychain: \"))\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")\n", + "\n", + "customer_orders = set()\n", + "\n", + "product1 = input(\"enter product 1: \")\n", + "customer_orders.add(product1)\n", + "product2 = input(\"enter product 2: \")\n", + "customer_orders.add(product2)\n", + "product3 = input(\"enter product 3: \")\n", + "customer_orders.add(product3)\n", + "\n", + "print(\"customer orders:\", customer_orders)\n", + "\n", + "total_products_sold = len(customer_orders)\n", + "print (total_products_sold)\n", + "total_available_products = len(products)\n", + "percentage_of_product_types_sold = (total_products_sold / total_available_products) * 100\n", + "print (percentage_of_product_types_sold, \"%\")\n", + "total_inventory_volume = sum(inventory.values())\n", + "total_products_sold = len(customer_orders)\n", + "percentage_sold = (total_products_sold / total_inventory_volume) * 100\n", + "print (percentage_sold, \"%\")\n", + "\n", + "order_status = tuple(customer_orders)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total_products_sold)\n", + "print(\"Percentage of Products Ordered:\", percentage_sold, \"%\")\n", + "\n", + "inventory[\"tshirt\"] = inventory[\"tshirt\"] - 1\n", + "inventory[\"mug\"] = inventory[\"mug\"] - 1\n", + "inventory[\"hat\"] = inventory[\"hat\"] - 1\n", + "inventory[\"book\"] = inventory[\"book\"] - 1\n", + "inventory[\"keychain\"] = inventory[\"keychain\"] - 1\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for product in inventory:\n", + " print(f\"{product}: {inventory[product]}\")\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -68,7 +156,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4, diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb new file mode 100644 index 00000000..08b92124 --- /dev/null +++ b/lab-python-error-handling.ipynb @@ -0,0 +1,180 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b", + "metadata": {}, + "source": [ + "## Exercise: Error Handling for Managing Customer Orders\n", + "\n", + "The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n", + "\n", + "For example, we could modify the `initialize_inventory` function to include error handling.\n", + " - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n", + "\n", + "```python\n", + "# Step 1: Define the function for initializing the inventory with error handling\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "# Or, in another way:\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory\n", + "```\n", + "\n", + "Let's enhance your code by implementing error handling to handle invalid inputs.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "2. Modify the `calculate_total_price` function to include error handling.\n", + " - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n", + "\n", + "3. Modify the `get_customer_orders` function to include error handling.\n", + " - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n", + " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. *Hint: you will need to pass inventory as a parameter*\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n", + "\n", + "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "aa46ccee", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'apple': 200, 'banana': 100, 'orange': 200}\n", + "['apple', 'banana', 'orange']\n", + "37.0\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity <= 0:\n", + " raise ValueError\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError:\n", + " print(\"Error: you must enter a positive value for an order.\")\n", + " return inventory\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total = 0\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter price for {product}: \"))\n", + " if price <= 0:\n", + " raise ValueError\n", + " total += price\n", + " break\n", + " except ValueError:\n", + " print(\"Error: you must enter a positive value for price.\")\n", + " return total\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " orders = []\n", + " while True:\n", + " try:\n", + " n = int(input(\"Enter number of orders: \"))\n", + " if n <= 0:\n", + " raise ValueError\n", + " break\n", + " except ValueError:\n", + " print(\"Error: you must enter a positive value for an order.\")\n", + "\n", + " for _ in range(n):\n", + " while True:\n", + " product = input(\"Enter product name: \")\n", + " if product not in inventory:\n", + " print(\"Error: you must enter a valid product name.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Error: Product out of stock.\")\n", + " else:\n", + " orders.append(product)\n", + " break\n", + " \n", + " return orders\n", + "\n", + "\n", + "products = [\"apple\", \"banana\", \"orange\"]\n", + "inventory = initialize_inventory(products)\n", + "orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price(orders)\n", + "\n", + "print(inventory)\n", + "print(orders)\n", + "print(total_price)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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-flow-control.ipynb b/lab-python-flow-control.ipynb new file mode 100644 index 00000000..b87e044f --- /dev/null +++ b/lab-python-flow-control.ipynb @@ -0,0 +1,109 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f", + "metadata": { + "tags": [] + }, + "source": [ + "# Lab | Flow Control" + ] + }, + { + "cell_type": "markdown", + "id": "3851fcd1-cf98-4653-9c89-e003b7ec9400", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized\n", + "\n", + "In the last lab, you were starting an online store that sells various products. To ensure smooth operations, you developed a program that manages customer orders and inventory.\n", + "\n", + "You did so without using flow control. Let's go a step further and improve this code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Look at your code from the lab data structures, and improve repeated code with loops.\n", + "\n", + "2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n", + " \n", + " a. Prompt the user to enter the name of a product that a customer wants to order.\n", + " \n", + " b. Add the product name to the \"customer_orders\" set.\n", + " \n", + " c. Ask the user if they want to add another product (yes/no).\n", + " \n", + " d. Continue the loop until the user does not want to add another product.\n", + "\n", + "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "367d3660", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['tshirt', 'mug', 'hat', 'book', 'keychain']\n", + "Available products: ['tshirt', 'mug', 'hat', 'book', 'keychain']\n", + "Updated inventory: \n", + "{'tshirt': 10, 'mug': 9, 'hat': 9, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "\n", + "for product in products:\n", + " quantity = int(input(\"Enter quantity for \" + product + \": \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "print(\"Available products:\", products)\n", + "product = input(\"Enter product name you want to order: \").lower()\n", + "customer_orders.add(product)\n", + "\n", + "answer = input(\"Do you want to add another product? (Yes/No): \").lower()\n", + "\n", + "while answer == \"yes\":\n", + " print(\"Available products:\", products)\n", + " product = input(\"Enter product name you want to order: \").lower()\n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want to add another product? (Yes/No): \").lower()\n", + "\n", + "for product in customer_orders:\n", + " inventory[product] -= 1\n", + "\n", + "print(\"Updated inventory: \")\n", + "print(inventory)" + ] + } + ], + "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.14.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions 2.ipynb b/lab-python-functions 2.ipynb new file mode 100644 index 00000000..16a24d44 --- /dev/null +++ b/lab-python-functions 2.ipynb @@ -0,0 +1,142 @@ +{ + "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": 3, + "id": "fa9e1538", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['tshirt', 'mug', 'hat', 'book', 'keychain']\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0 %\n", + "Updated inventory:\n", + "{'tshirt': 10, 'mug': 9, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(\"Enter quantity for \" + product + \": \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " print(\"Available products:\", products)\n", + " product = input(\"Enter product name you want to order: \").lower()\n", + " customer_orders.add(product)\n", + " \n", + " answer = input(\"Do you want to add another product? (Yes/No): \").lower()\n", + " \n", + " while answer == \"yes\":\n", + " print(\"Available products:\", products)\n", + " product = input(\"Enter product name you want to order: \").lower()\n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want to add another product? (Yes/No): \").lower()\n", + " \n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\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\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage, \"%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated inventory:\")\n", + " print(inventory)\n", + "\n", + "# Main program\n", + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\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", + "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.14.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb new file mode 100644 index 00000000..16a24d44 --- /dev/null +++ b/lab-python-functions.ipynb @@ -0,0 +1,142 @@ +{ + "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": 3, + "id": "fa9e1538", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Available products: ['tshirt', 'mug', 'hat', 'book', 'keychain']\n", + "Order Statistics:\n", + "Total Products Ordered: 1\n", + "Percentage of Products Ordered: 20.0 %\n", + "Updated inventory:\n", + "{'tshirt': 10, 'mug': 9, 'hat': 10, 'book': 10, 'keychain': 10}\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(\"Enter quantity for \" + product + \": \"))\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " print(\"Available products:\", products)\n", + " product = input(\"Enter product name you want to order: \").lower()\n", + " customer_orders.add(product)\n", + " \n", + " answer = input(\"Do you want to add another product? (Yes/No): \").lower()\n", + " \n", + " while answer == \"yes\":\n", + " print(\"Available products:\", products)\n", + " product = input(\"Enter product name you want to order: \").lower()\n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want to add another product? (Yes/No): \").lower()\n", + " \n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\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\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage = order_statistics\n", + " print(\"Order Statistics:\")\n", + " print(\"Total Products Ordered:\", total_ordered)\n", + " print(\"Percentage of Products Ordered:\", percentage, \"%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated inventory:\")\n", + " print(inventory)\n", + "\n", + "# Main program\n", + "products = [\"tshirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\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", + "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.14.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb new file mode 100644 index 00000000..ca1ff825 --- /dev/null +++ b/lab-python-list-comprehension.ipynb @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | List, Dict and Set Comprehension" + ] + }, + { + "cell_type": "markdown", + "id": "7dd3cbde-675a-4b81-92c3-f728846dbe06", + "metadata": {}, + "source": [ + "## Exercise: Managing Customer Orders Optimized with Comprehension" + ] + }, + { + "cell_type": "markdown", + "id": "5d500160-2fb7-4777-b5e4-09d45ebaf328", + "metadata": {}, + "source": [ + "In the previous exercise, you developed a program to manage customer orders and inventory. Now, let's take it a step further and incorporate comprehension into your code.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n", + "\n", + " - *Hint: Apply it to initialize inventory, updating the inventory and printing the updated inventory.*\n", + " \n", + " - For example, in initializing the inventory, we could have:\n", + " \n", + " ```python\n", + " def initialize_inventory(products):\n", + " inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", + " return inventory\n", + "\n", + " ```\n", + "
\n", + " \n", + " \n", + "2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input. Use comprehension.\n", + "\n", + "3. Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n", + "\n", + "4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory.\n", + "\n", + "5. Print the total price of the customer order.\n", + "\n", + "Your code should produce output similar to the following:\n", + "\n", + "```python\n", + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 2\n", + "Enter the quantity of keychains available: 1\n", + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: keychain\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 2\n", + "book: 2\n", + "Enter the price of keychain: 5\n", + "Enter the price of hat: 10\n", + "Total Price: 15.0\n", + "\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d9dc6ece", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 100\n", + "mug: 99\n", + "hat: 99\n", + "book: 100\n", + "keychain: 100\n", + "Total Price: 33.0\n" + ] + } + ], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {product: int(input(\"Enter the quantity of \" + product + \"s available: \")) for product in products}\n", + " return inventory\n", + "\n", + "def get_customer_orders():\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " customer_orders = {input(\"Enter the name of a product that a customer wants to order: \").lower() for i in range(num_orders)}\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] = inventory[product] - 1\n", + " \n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " return inventory\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\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total_ordered, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(\"Total Products Ordered:\", total_ordered)\n", + " print(\"Percentage of Unique Products Ordered:\", percentage)\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product in inventory:\n", + " print(product + \":\", inventory[product])\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = [float(input(\"Enter the price of \" + product + \": \")) for product in customer_orders]\n", + " total_price = sum(prices)\n", + " return total_price\n", + "\n", + "# Main program\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders()\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(\"Total Price:\", total_price)" + ] + } + ], + "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.14.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}