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..3bfc264 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,344 @@ +{ + "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": 5, + "id": "ee849abd-4151-4884-8f5a-9caa5fd46c4f", + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = ()\n", + "percentage_ordered = ()\n", + "inventory = {}\n", + "customer_orders = {}\n", + "\n", + "#1\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", + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " if inventory[product] == 0:\n", + " print(f\"'{product}' is now out of stock.\")\n", + " else:\n", + " print(f\"'{product} doesn't exist or is out of stock.\")\n", + " return inventory\n", + "\n", + "#4\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", + "#5\n", + "def print_order_statistics(total_products_ordered, percentage_ordered):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "#6\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0cc7a9b7-3810-45a8-9918-da5ffcfae62c", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "6c2c2f83-0849-481c-9408-f95357617e89", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders(products):\n", + " customer_orders = set() \n", + " try:\n", + " num_products = int(input(\"How many products does the customer want to order? \").strip())\n", + " if num_products < 0:\n", + " raise ValueError(\"The number of products cannot be negative.\")\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return customer_orders\n", + " \n", + " print(\"\\nEnter the names of the products the customer wants to order.\")\n", + " for _ in range(num_products):\n", + " while True:\n", + " product_name = input(\"Enter a product name: \").strip().lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " break\n", + " else:\n", + " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d0569626-0acf-46be-ac90-93e7fc52da8b", + "metadata": {}, + "outputs": [], + "source": [ + "#7\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"enter the prize of {product}\"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"the price cannot be negative!\")\n", + " except ValueError:\n", + " print(f\"Error: invalid input - {error}\")\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "3dc0726d-11ea-4b53-8af6-23629c243962", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 5\n", + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 5\n", + "How many products does the customer want to order? 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter the names of the products the customer wants to order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'book' added to the order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: mmug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'mmug' is not a valid product. Please choose from {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'mug' added to the order.\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 5\n", + "book: 4\n", + "keychain: 5\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the prize of book 10\n", + "enter the prize of mug 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total price: 12.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(total_products_ordered, percentage_ordered)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"total price: {total_price}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f6daced-59f3-4d15-936a-bc15c134b49f", + "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.12.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..3bfc264 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,252 @@ "\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": 5, + "id": "ee849abd-4151-4884-8f5a-9caa5fd46c4f", + "metadata": {}, + "outputs": [], + "source": [ + "total_products_ordered = ()\n", + "percentage_ordered = ()\n", + "inventory = {}\n", + "customer_orders = {}\n", + "\n", + "#1\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", + "#3\n", + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " if inventory[product] == 0:\n", + " print(f\"'{product}' is now out of stock.\")\n", + " else:\n", + " print(f\"'{product} doesn't exist or is out of stock.\")\n", + " return inventory\n", + "\n", + "#4\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", + "#5\n", + "def print_order_statistics(total_products_ordered, percentage_ordered):\n", + " print(\"Order Statistics:\")\n", + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n", + "\n", + "#6\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "markdown", + "id": "0cc7a9b7-3810-45a8-9918-da5ffcfae62c", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "6c2c2f83-0849-481c-9408-f95357617e89", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def get_customer_orders(products):\n", + " customer_orders = set() \n", + " try:\n", + " num_products = int(input(\"How many products does the customer want to order? \").strip())\n", + " if num_products < 0:\n", + " raise ValueError(\"The number of products cannot be negative.\")\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return customer_orders\n", + " \n", + " print(\"\\nEnter the names of the products the customer wants to order.\")\n", + " for _ in range(num_products):\n", + " while True:\n", + " product_name = input(\"Enter a product name: \").strip().lower()\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"'{product_name}' added to the order.\")\n", + " break\n", + " else:\n", + " print(f\"'{product_name}' is not a valid product. Please choose from {products}.\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d0569626-0acf-46be-ac90-93e7fc52da8b", + "metadata": {}, + "outputs": [], + "source": [ + "#7\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"enter the prize of {product}\"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"the price cannot be negative!\")\n", + " except ValueError:\n", + " print(f\"Error: invalid input - {error}\")\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "3dc0726d-11ea-4b53-8af6-23629c243962", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 5\n", + "Enter the quantity of books available: 5\n", + "Enter the quantity of keychains available: 5\n", + "How many products does the customer want to order? 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Enter the names of the products the customer wants to order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'book' added to the order.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: mmug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'mmug' is not a valid product. Please choose from {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a product name: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "'mug' added to the order.\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Products Ordered: 40.00%\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 5\n", + "book: 4\n", + "keychain: 5\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "enter the prize of book 10\n", + "enter the prize of mug 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total price: 12.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "total_products_ordered, percentage_ordered = calculate_order_statistics(customer_orders, products)\n", + "\n", + "print_order_statistics(total_products_ordered, percentage_ordered)\n", + "\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"total price: {total_price}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f6daced-59f3-4d15-936a-bc15c134b49f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +336,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,