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..746df73 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,332 @@ +{ + "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": "78ec8891-96fd-407c-bdcc-adacfed41c86", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(f\"List of products: {products}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "94119890-141d-49a3-8a42-51e57f92d308", + "metadata": {}, + "outputs": [], + "source": [ + "#1 initialize_inventory (with error handling)\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\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", + " inventory[product] = quantity\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e35b4b5e-5bc1-4107-9c88-a47111e34e39", + "metadata": {}, + "outputs": [], + "source": [ + "#2 calculate_total_price (with error handling)\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\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", + " total_price += price\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "733f1d24-6c3f-463e-94bd-7541ef56f7ce", + "metadata": {}, + "outputs": [], + "source": [ + "#3 get_customer_orders (with error handling)\n", + "def get_customer_orders(inventory):\n", + " # Ask for number of orders\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Invalid number! Please enter a non-negative integer.\")\n", + " valid_number = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " # Ask for each product\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\n", + " f\"Please enter the product name for order #{i+1}: \"\n", + " ).strip()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\n", + " \"Invalid product name! This product is not in the inventory.\"\n", + " )\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\n", + " \"Invalid product! This product is out of stock.\"\n", + " )\n", + "\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5098e49f-27ad-4162-ae6c-e4ae4fdd264b", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d3e6b3e6-fc72-48fc-9d7d-8844558d18e7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 1\n", + "Enter the quantity of mugs available: 2\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 4\n", + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory: {'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 3\n", + "Please enter the product name for order #1: book\n", + "Please enter the product name for order #2: keychain\n", + "Please enter the product name for order #3: jj\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid product name! This product is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the product name for order #3: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer orders: {'keychain', 'book', 'hat'}\n", + "\n", + "Updated inventory: {'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 3, 'keychain': 4}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 56\n", + "Enter the price of book: 45\n", + "Enter the price of hat: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Price: 107.0\n" + ] + } + ], + "source": [ + "#4 TEST\n", + "inventory = initialize_inventory(products)\n", + "print(\"\\nInventory:\", inventory)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(\"\\nCustomer orders:\", customer_orders)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print(\"\\nUpdated inventory:\", inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"\\nTotal Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a26d41ad-bd80-4861-997d-3649bca1e5ed", + "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-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..746df73 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,240 @@ "\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": "78ec8891-96fd-407c-bdcc-adacfed41c86", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List of products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(f\"List of products: {products}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "94119890-141d-49a3-8a42-51e57f92d308", + "metadata": {}, + "outputs": [], + "source": [ + "#1 initialize_inventory (with error handling)\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\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", + " inventory[product] = quantity\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e35b4b5e-5bc1-4107-9c88-a47111e34e39", + "metadata": {}, + "outputs": [], + "source": [ + "#2 calculate_total_price (with error handling)\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\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", + " total_price += price\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "733f1d24-6c3f-463e-94bd-7541ef56f7ce", + "metadata": {}, + "outputs": [], + "source": [ + "#3 get_customer_orders (with error handling)\n", + "def get_customer_orders(inventory):\n", + " # Ask for number of orders\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " raise ValueError(\"Invalid number! Please enter a non-negative integer.\")\n", + " valid_number = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " # Ask for each product\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\n", + " f\"Please enter the product name for order #{i+1}: \"\n", + " ).strip()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\n", + " \"Invalid product name! This product is not in the inventory.\"\n", + " )\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\n", + " \"Invalid product! This product is out of stock.\"\n", + " )\n", + "\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5098e49f-27ad-4162-ae6c-e4ae4fdd264b", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " inventory[product] -= 1\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d3e6b3e6-fc72-48fc-9d7d-8844558d18e7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 1\n", + "Enter the quantity of mugs available: 2\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 4\n", + "Enter the quantity of keychains available: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory: {'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 3\n", + "Please enter the product name for order #1: book\n", + "Please enter the product name for order #2: keychain\n", + "Please enter the product name for order #3: jj\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid product name! This product is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the product name for order #3: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer orders: {'keychain', 'book', 'hat'}\n", + "\n", + "Updated inventory: {'t-shirt': 1, 'mug': 2, 'hat': 2, 'book': 3, 'keychain': 4}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 56\n", + "Enter the price of book: 45\n", + "Enter the price of hat: 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Price: 107.0\n" + ] + } + ], + "source": [ + "#4 TEST\n", + "inventory = initialize_inventory(products)\n", + "print(\"\\nInventory:\", inventory)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(\"\\nCustomer orders:\", customer_orders)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print(\"\\nUpdated inventory:\", inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"\\nTotal Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a26d41ad-bd80-4861-997d-3649bca1e5ed", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +324,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,