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..1c37b9c --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,311 @@ +{ + "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": "17fc5f42-e9c0-4c03-a18e-681c8b087006", + "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": null, + "id": "7f08c927-86e7-4f2d-a010-f811ca08e6e9", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " total_price = 0\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price for each {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", + " total_price += price\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92bff43e-bae3-4ac4-a7c9-d24da547818f", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " \n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " \n", + " if num_orders > 0:\n", + " break\n", + " else:\n", + " print(\"Please enter a number greater than 0.\")\n", + " except:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " \n", + " customer_orders = []\n", + " \n", + " for i in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " \n", + " if product not in inventory:\n", + " print(\"This product is not in the inventory. Try again.\")\n", + " \n", + " elif inventory[product] == 0:\n", + " print(f\"Sorry, {product} is out of stock. Please choose another item.\")\n", + "\n", + " else:\n", + " customer_orders.append(product)\n", + " break \n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a2ea5b6b-d68e-4b9b-917d-aa652a55b7c9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirts: -2\n", + "Quantity of mugs: 5\n", + "Quantity of hats: 6\n", + "Quantity of books: 7\n", + "Quantity of keychains: 7\n", + "How many orders? 3\n", + "Product name: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid or out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product name: hat\n", + "Product name: mug\n", + "Product name: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total ordered: 3\n", + "percentage ordered %: 60.0\n", + "Updated Inventory:\n", + "mug: 4\n", + "hat: 5\n", + "book: 6\n", + "keychain: 7\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Price of hat: 10\n", + "Price of mug: 7\n", + "Price of book: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 24.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " return {\n", + " product: int(input(f\"Quantity of {product}s: \"))\n", + " for product in products\n", + " }\n", + "\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " num = int(input(\"How many orders? \"))\n", + " if num > 0:\n", + " break\n", + " print(\"Enter a number > 0.\")\n", + " except:\n", + " print(\"Invalid number.\")\n", + "\n", + " orders = []\n", + " for _ in range(num):\n", + " while True:\n", + " product = input(\"Product name: \").strip().lower()\n", + " if product in inventory and inventory[product] > 0:\n", + " orders.append(product)\n", + " break\n", + " print(\"Invalid or out of stock.\")\n", + " return orders\n", + "\n", + "def update_inventory(inventory, orders):\n", + " for product in orders:\n", + " inventory[product] -= 1\n", + " return {p: q for p, q in inventory.items() if q > 0}\n", + "\n", + "def calculate_total_price(orders):\n", + " return sum(float(input(f\"Price of {p}: \")) for p in orders)\n", + "\n", + "inventory = initialize_inventory(products)\n", + "orders = get_customer_orders(inventory)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(\"Total ordered:\", len(orders))\n", + "print(\"percentage ordered %:\", round(len(set(orders)) / len(products) * 100, 1))\n", + "\n", + "inventory = update_inventory(inventory, orders)\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for p, q in inventory.items():\n", + " print(f\"{p}: {q}\")\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total Price:\", total)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8673b06-5390-4847-b6a8-48159e19f5be", + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..1c37b9c 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,219 @@ "\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": "17fc5f42-e9c0-4c03-a18e-681c8b087006", + "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": null, + "id": "7f08c927-86e7-4f2d-a010-f811ca08e6e9", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " total_price = 0\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price for each {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", + " total_price += price\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92bff43e-bae3-4ac4-a7c9-d24da547818f", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " \n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " \n", + " if num_orders > 0:\n", + " break\n", + " else:\n", + " print(\"Please enter a number greater than 0.\")\n", + " except:\n", + " print(\"Invalid input. Please enter a number.\")\n", + " \n", + " customer_orders = []\n", + " \n", + " for i in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " \n", + " if product not in inventory:\n", + " print(\"This product is not in the inventory. Try again.\")\n", + " \n", + " elif inventory[product] == 0:\n", + " print(f\"Sorry, {product} is out of stock. Please choose another item.\")\n", + "\n", + " else:\n", + " customer_orders.append(product)\n", + " break \n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "a2ea5b6b-d68e-4b9b-917d-aa652a55b7c9", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Quantity of t-shirts: -2\n", + "Quantity of mugs: 5\n", + "Quantity of hats: 6\n", + "Quantity of books: 7\n", + "Quantity of keychains: 7\n", + "How many orders? 3\n", + "Product name: t-shirt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid or out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product name: hat\n", + "Product name: mug\n", + "Product name: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order Statistics:\n", + "Total ordered: 3\n", + "percentage ordered %: 60.0\n", + "Updated Inventory:\n", + "mug: 4\n", + "hat: 5\n", + "book: 6\n", + "keychain: 7\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Price of hat: 10\n", + "Price of mug: 7\n", + "Price of book: 7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total Price: 24.0\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " return {\n", + " product: int(input(f\"Quantity of {product}s: \"))\n", + " for product in products\n", + " }\n", + "\n", + "def get_customer_orders(inventory):\n", + " while True:\n", + " try:\n", + " num = int(input(\"How many orders? \"))\n", + " if num > 0:\n", + " break\n", + " print(\"Enter a number > 0.\")\n", + " except:\n", + " print(\"Invalid number.\")\n", + "\n", + " orders = []\n", + " for _ in range(num):\n", + " while True:\n", + " product = input(\"Product name: \").strip().lower()\n", + " if product in inventory and inventory[product] > 0:\n", + " orders.append(product)\n", + " break\n", + " print(\"Invalid or out of stock.\")\n", + " return orders\n", + "\n", + "def update_inventory(inventory, orders):\n", + " for product in orders:\n", + " inventory[product] -= 1\n", + " return {p: q for p, q in inventory.items() if q > 0}\n", + "\n", + "def calculate_total_price(orders):\n", + " return sum(float(input(f\"Price of {p}: \")) for p in orders)\n", + "\n", + "inventory = initialize_inventory(products)\n", + "orders = get_customer_orders(inventory)\n", + "\n", + "print(\"Order Statistics:\")\n", + "print(\"Total ordered:\", len(orders))\n", + "print(\"percentage ordered %:\", round(len(set(orders)) / len(products) * 100, 1))\n", + "\n", + "inventory = update_inventory(inventory, orders)\n", + "\n", + "print(\"Updated Inventory:\")\n", + "for p, q in inventory.items():\n", + " print(f\"{p}: {q}\")\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total Price:\", total)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8673b06-5390-4847-b6a8-48159e19f5be", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +303,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4,