diff --git a/lab-python-error-handling 2.ipynb b/lab-python-error-handling 2.ipynb new file mode 100644 index 0000000..b303c28 --- /dev/null +++ b/lab-python-error-handling 2.ipynb @@ -0,0 +1,231 @@ +{ + "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": null, + "id": "f8e836a5", + "metadata": {}, + "outputs": [], + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f3196ec0", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " prices = {}\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\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", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " prices[product] = price\n", + " return prices\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0891242a", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = {}\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of different products to order: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"Invalid number! Please enter a non-negative value.\")\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " for _ in range(num_orders):\n", + " while True:\n", + " product_name = input(\"Enter the product name: \")\n", + " if product_name not in inventory:\n", + " print(\"Error: This product is not in the inventory. Please enter a valid product name.\")\n", + " continue\n", + "\n", + " if inventory[product_name] <= 0:\n", + " print(f\"Error: {product_name} is out of stock.\")\n", + " continue\n", + "\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity for {product_name}: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " elif quantity > inventory[product_name]:\n", + " print(f\"Error: Only {inventory[product_name]} {product_name}s are available.\")\n", + " else:\n", + " orders[product_name] = quantity\n", + " inventory[product_name] -= quantity\n", + " break\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "fcdd7fb7", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "list indices must be integers or slices, not str", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[4], line 10\u001b[0m\n\u001b[0;32m 7\u001b[0m prices \u001b[38;5;241m=\u001b[39m calculate_total_price(products)\n\u001b[0;32m 9\u001b[0m \u001b[38;5;66;03m# Step 3: Get customer orders with error handling\u001b[39;00m\n\u001b[1;32m---> 10\u001b[0m orders \u001b[38;5;241m=\u001b[39m \u001b[43mget_customer_orders\u001b[49m\u001b[43m(\u001b[49m\u001b[43minventory\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 12\u001b[0m \u001b[38;5;66;03m# Display final order summary\u001b[39;00m\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mOrder Summary:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "Cell \u001b[1;32mIn[2], line 19\u001b[0m, in \u001b[0;36mget_customer_orders\u001b[1;34m(inventory)\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: This product is not in the inventory. Please enter a valid product name.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 17\u001b[0m \u001b[38;5;28;01mcontinue\u001b[39;00m\n\u001b[1;32m---> 19\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43minventory\u001b[49m\u001b[43m[\u001b[49m\u001b[43mproduct_name\u001b[49m\u001b[43m]\u001b[49m \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 20\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mproduct_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is out of stock.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 21\u001b[0m \u001b[38;5;28;01mcontinue\u001b[39;00m\n", + "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str" + ] + } + ], + "source": [ + "products = [\"apple\", \"banana\", \"orange\"]\n", + "\n", + "# Step 1: Initialize inventory with error handling\n", + "inventory = products\n", + "\n", + "# Step 2: Set prices with error handling\n", + "prices = calculate_total_price(products)\n", + "\n", + "# Step 3: Get customer orders with error handling\n", + "orders = get_customer_orders(inventory)\n", + "\n", + "# Display final order summary\n", + "print(\"\\nOrder Summary:\")\n", + "total_cost = 0\n", + "for product, quantity in orders.items():\n", + " cost = prices[product] * quantity\n", + " total_cost += cost\n", + " print(f\"{quantity} x {product} @ {prices[product]} each = {cost}\")\n", + "\n", + "print(f\"Total Cost: {total_cost}\")\n" + ] + } + ], + "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.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb deleted file mode 100644 index f4c6ef6..0000000 --- a/lab-python-error-handling.ipynb +++ /dev/null @@ -1,98 +0,0 @@ -{ - "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 -}