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..388f402 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,442 @@ +{ + "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": "dd064a42-1336-4760-9f0c-533176642a1a", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: jf\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'jf'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 14ik\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '14ik'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: f0ifkds\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'f0ifkds'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 104u29je\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '104u29je'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 0acs\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '0acs'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 1\n", + "Enter the quantity of mugs available: a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'a'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "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", + "Enter the number of customer orders: 6\n", + "Enter the name of a product that a customer wants to order: fdoja\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'fdoja' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: NAIC\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'naic' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: !\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: '!' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: '0' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'no' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: none\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'none' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: tshirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'tshirt' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: t-shirt\n", + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Unique Products Ordered: 60.0\n", + "\n", + "Updated Inventory:\n", + "mug: 1\n", + "hat: 2\n", + "book: 4\n", + "keychain: 5\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: could not convert string to float: 'book'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 1\n", + "Enter the price of hat: 2\n", + "Enter the price of mug: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 6.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\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", + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " if product not in inventory:\n", + " raise ValueError(f\"'{product}' is not in the inventory.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(f\"'{product}' is out of stock.\")\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " updated = {product: (qty - 1 if product in customer_orders else qty)\n", + " for product, qty in inventory.items()}\n", + " return {product: qty for product, qty in updated.items() if qty > 0}\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, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage}\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " print(\"\\n\".join(f\"{product}: {qty}\" for product, qty in inventory.items()))\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\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(\"Price cannot be negative.\")\n", + " total_price += price\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return total_price\n", + "\n", + "# Main program\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\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(f\"Total Price: {total_price}\")" + ] + } + ], + "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.14.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..388f402 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,350 @@ "\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": "dd064a42-1336-4760-9f0c-533176642a1a", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: jf\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'jf'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 14ik\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '14ik'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: f0ifkds\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'f0ifkds'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 104u29je\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '104u29je'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 0acs\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '0acs'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 1\n", + "Enter the quantity of mugs available: a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'a'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "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", + "Enter the number of customer orders: 6\n", + "Enter the name of a product that a customer wants to order: fdoja\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'fdoja' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: NAIC\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'naic' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: !\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: '!' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: '0' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'no' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: none\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'none' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: tshirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'tshirt' is not in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: t-shirt\n", + "Enter the name of a product that a customer wants to order: mug\n", + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Unique Products Ordered: 60.0\n", + "\n", + "Updated Inventory:\n", + "mug: 1\n", + "hat: 2\n", + "book: 4\n", + "keychain: 5\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: could not convert string to float: 'book'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 1\n", + "Enter the price of hat: 2\n", + "Enter the price of mug: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 6.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\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", + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " if product not in inventory:\n", + " raise ValueError(f\"'{product}' is not in the inventory.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(f\"'{product}' is out of stock.\")\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return customer_orders\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " updated = {product: (qty - 1 if product in customer_orders else qty)\n", + " for product, qty in inventory.items()}\n", + " return {product: qty for product, qty in updated.items() if qty > 0}\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, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage}\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " print(\"\\n\".join(f\"{product}: {qty}\" for product, qty in inventory.items()))\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\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(\"Price cannot be negative.\")\n", + " total_price += price\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return total_price\n", + "\n", + "# Main program\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\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(f\"Total Price: {total_price}\")" + ] } ], "metadata": { @@ -90,7 +434,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.7" } }, "nbformat": 4,