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..dd66ced --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,172 @@ +{ + "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": "cee0f9b6-f6df-4c5f-8a14-0a414077b66b", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " \"\"\"\n", + " Calculates the total price by asking the user for the price of each ordered product.\n", + " Includes error handling for non-numeric and negative values.\n", + " \"\"\"\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " total_price += price\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Invalid input: {error}. Please try again.\")\n", + " return total_price\n", + "\n", + "def get_customer_orders(inventory):\n", + " \"\"\"\n", + " Gets product orders from the customer.\n", + " Includes error handling for:\n", + " 1. Invalid number of orders.\n", + " 2. Products not in inventory.\n", + " 3. Products out of stock.\n", + " \"\"\"\n", + " customer_orders = set()\n", + " \n", + "\n", + " valid_count = False\n", + " while not valid_count:\n", + " try:\n", + " num_orders = int(input(\"How many products do you want to order? \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"The number of orders cannot be negative.\")\n", + " valid_count = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}. Please enter a valid integer.\")\n", + "\n", + " \n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product_name = input(\"Enter the name of the product: \")\n", + " try:\n", + " if product_name not in inventory:\n", + " raise KeyError(f\"{product_name} is not available in our catalog.\")\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(f\"Sorry, {product_name} is out of stock.\")\n", + " \n", + " customer_orders.add(product_name)\n", + " valid_product = True\n", + " except (KeyError, ValueError) as error:\n", + " print(f\"Order error: {error}\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d56b8c00-e29a-4fc1-aca4-7ebc53ac6330", + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..dd66ced 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,80 @@ "\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": "cee0f9b6-f6df-4c5f-8a14-0a414077b66b", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " \"\"\"\n", + " Calculates the total price by asking the user for the price of each ordered product.\n", + " Includes error handling for non-numeric and negative values.\n", + " \"\"\"\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " total_price += price\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Invalid input: {error}. Please try again.\")\n", + " return total_price\n", + "\n", + "def get_customer_orders(inventory):\n", + " \"\"\"\n", + " Gets product orders from the customer.\n", + " Includes error handling for:\n", + " 1. Invalid number of orders.\n", + " 2. Products not in inventory.\n", + " 3. Products out of stock.\n", + " \"\"\"\n", + " customer_orders = set()\n", + " \n", + "\n", + " valid_count = False\n", + " while not valid_count:\n", + " try:\n", + " num_orders = int(input(\"How many products do you want to order? \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"The number of orders cannot be negative.\")\n", + " valid_count = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}. Please enter a valid integer.\")\n", + "\n", + " \n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product_name = input(\"Enter the name of the product: \")\n", + " try:\n", + " if product_name not in inventory:\n", + " raise KeyError(f\"{product_name} is not available in our catalog.\")\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(f\"Sorry, {product_name} is out of stock.\")\n", + " \n", + " customer_orders.add(product_name)\n", + " valid_product = True\n", + " except (KeyError, ValueError) as error:\n", + " print(f\"Order error: {error}\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d56b8c00-e29a-4fc1-aca4-7ebc53ac6330", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +164,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,