From 872ffd8d4b5ba64b7669707ba93a9faf533d14fd Mon Sep 17 00:00:00 2001 From: Francisca Date: Thu, 19 Feb 2026 15:58:05 +0000 Subject: [PATCH] Resolution error handling --- ...lab-python-error-handling-checkpoint.ipynb | 98 +++++++++++ lab-python-error-handling.ipynb | 165 +++++++++++++++++- 2 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb new file mode 100644 index 0000000..f4c6ef6 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,98 @@ +{ + "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" + ] + } + ], + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..af229d7 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,169 @@ "\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": 52, + "id": "e47153c6-cf84-43af-ba4a-7880b9769db7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 2\n", + "Enter the quantity of mugs available: 2\n", + "Enter the quantity of hats available: 2\n", + "Enter the quantity of books available: 2\n", + "Enter the quantity of keychains available: 2\n", + "Enter the number of customer orders: 3\n", + "Enter product name: mug\n", + "Enter product name: mug\n", + "Enter product name: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, mug is out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product name: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Products Ordered: 60.0 %\n", + "Updated Inventory: {'t-shirt': 2, 'mug': 0, 'hat': 1, 'book': 2, 'keychain': 2}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 5\n", + "Enter the price of hat: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total price of the order: 20.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Initialize inventory\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " while True:\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", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", + " return inventory\n", + "\n", + "# Get customer orders\n", + "\n", + "def get_customer_orders(products, inventory):\n", + " while True:\n", + " try: \n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"Invalid order! Please enter a non-negative value.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = {}\n", + " \n", + " for i in range(num_orders):\n", + " while True:\n", + " product_name = input(f\"Enter product name: \").strip().lower()\n", + "\n", + " if product_name not in products:\n", + " print(\"Product not available. Available products:\", products)\n", + " elif inventory[product_name] <= 0:\n", + " print(f\"Sorry, {product_name} is out of stock.\")\n", + " else:\n", + " if product_name in customer_orders:\n", + " customer_orders[product_name] += 1\n", + " else:\n", + " customer_orders[product_name] = 1\n", + "\n", + " inventory[product_name] -= 1\n", + " break\n", + " \n", + "\n", + " return customer_orders\n", + "\n", + "# Order statistics\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = sum(customer_orders.values())\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered \n", + "\n", + "# Total price\n", + "\n", + "def total_price(customer_orders):\n", + " total = 0\n", + " for product, quantity in customer_orders.items():\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " total += price * quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return total\n", + "\n", + "# main program\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(products, inventory)\n", + "\n", + "total, percentage = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(\"Total Products Ordered:\", total)\n", + "print(\"Percentage of Products Ordered:\", percentage, \"%\")\n", + "\n", + "print(f\"Updated Inventory:\", inventory)\n", + "\n", + "print(f\"Total price of the order:\", total_price(customer_orders))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "45f2676d-3579-4fcb-b8f4-7e59e52064c6", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +253,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,