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..bf49626 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,462 @@ +{ + "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": "markdown", + "id": "6f617a39-d2d0-485e-8305-c000b7d2d97d", + "metadata": {}, + "source": [ + "2. Modify the calculate_total_price function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "6cb1e248-41a5-49e8-9bd1-e794f9f7d81c", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_price = False \n", + "\n", + " while valid_price == False:\n", + " try:\n", + " price_product = int(input(f\"Enter the price of {product}: \")) \n", + "\n", + " if price_product < 0: # only blocks negative prices\n", + " raise ValueError(\"Negative prices are not allowed.\")\n", + "\n", + " valid_price = True\n", + " total += price_product\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid price: {error}\")\n", + " print(\"Please enter a positive numeric value\")\n", + "\n", + " return total" + ] + }, + { + "cell_type": "markdown", + "id": "0cd21824-2368-4661-8a71-098a35d951e1", + "metadata": {}, + "source": [ + "3. Modify the get_customer_orders function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "28c1e666-dd2a-4033-9f2c-46ed8312214a", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = []\n", + "\n", + " valid_number = False\n", + " while valid_number == False:\n", + " try:\n", + " num_orders = int(input(\"How many products do you want to order? \"))\n", + "\n", + " if num_orders <= 0:\n", + " raise ValueError(\"Number of orders must be positive.\")\n", + "\n", + " valid_number = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid number: {error}\")\n", + " print(\"Please enter a positive numeric value.\")\n", + "\n", + " for num in range(num_orders):\n", + " valid_product = False\n", + "\n", + " while valid_product == False:\n", + " try:\n", + " product_name = input(\"Please enter the name of your product: \")\n", + "\n", + " if product_name not in inventory:\n", + " raise ValueError(\"Product not found in inventory.\")\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + "\n", + " valid_product = True\n", + " orders.append(product_name)\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid product: {error}\")\n", + " print(\"Please enter a valid product name.\")\n", + "\n", + " return orders" + ] + }, + { + "cell_type": "markdown", + "id": "bb4729b0-53ab-44f9-b492-cf7ec6ce8a3c", + "metadata": {}, + "source": [ + "4. Test your code by running the program " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c7116dcf-02dc-4be6-a19d-15bb9f9d5df9", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 3\n", + "Please enter the name of your product: t-shirt\n", + "Please enter the name of your product: hut\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product not found in inventory.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: book\n", + "Please enter the name of your product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product is out of stock.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orders: ['t-shirt', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 3\n", + "Enter the price of book: -5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price: Negative prices are not allowed.\n", + "Please enter a positive numeric value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: 0\n", + "Enter the price of keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total: 13\n" + ] + } + ], + "source": [ + "# first try\n", + "inventory = {\n", + " \"t-shirt\": 345,\n", + " \"mug\": 0,\n", + " \"hat\": 56,\n", + " \"book\": 9,\n", + " \"keychain\": 1\n", + "}\n", + "\n", + "orders = get_customer_orders(inventory)\n", + "print(\"Orders:\", orders)\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total:\", total)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed0e71de-9de6-46d0-8d80-1ac3f45f75bf", + "metadata": {}, + "outputs": [], + "source": [ + "# above, if the price of book is zero, the program allowed it \n", + "# so i had to change the validation condition (if price_product < 0 to if price_product <= 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "309a531a-4b88-4fd3-a4a7-c9a9c2881e79", + "metadata": {}, + "outputs": [], + "source": [ + "# correção:\n", + "def calculate_total_price(customer_orders):\n", + " total = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_price = False \n", + "\n", + " while valid_price == False:\n", + " try:\n", + " price_product = int(input(f\"Enter the price of {product}: \")) \n", + "\n", + " if price_product <= 0: # blocking zero and negatives\n", + " raise ValueError(\"Zero and Negative prices are not allowed.\")\n", + "\n", + " valid_price = True\n", + " total += price_product\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid price: {error}\")\n", + " print(\"Please enter a positive numeric value\")\n", + "\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0f02ea82-22b0-4a45-965c-5c1cd8c731fc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 3\n", + "Please enter the name of your product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product is out of stock.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: pen\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product not found in inventory.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: keychain\n", + "Please enter the name of your product: t-shirt\n", + "Please enter the name of your product: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orders: ['keychain', 't-shirt', 'book']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price: Zero and Negative prices are not allowed.\n", + "Please enter a positive numeric value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: -45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price: Zero and Negative prices are not allowed.\n", + "Please enter a positive numeric value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 90\n", + "Enter the price of t-shirt: 5\n", + "Enter the price of book: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total: 119\n" + ] + } + ], + "source": [ + "# second try \n", + "inventory = {\n", + " \"t-shirt\": 345,\n", + " \"mug\": 0,\n", + " \"hat\": 56,\n", + " \"book\": 9,\n", + " \"keychain\": 1\n", + "}\n", + "\n", + "orders = get_customer_orders(inventory)\n", + "print(\"Orders:\", orders)\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total:\", total)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:anaconda3]", + "language": "python", + "name": "conda-env-anaconda3-py" + }, + "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.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..bf49626 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,377 @@ "\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": "markdown", + "id": "6f617a39-d2d0-485e-8305-c000b7d2d97d", + "metadata": {}, + "source": [ + "2. Modify the calculate_total_price function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "6cb1e248-41a5-49e8-9bd1-e794f9f7d81c", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_price = False \n", + "\n", + " while valid_price == False:\n", + " try:\n", + " price_product = int(input(f\"Enter the price of {product}: \")) \n", + "\n", + " if price_product < 0: # only blocks negative prices\n", + " raise ValueError(\"Negative prices are not allowed.\")\n", + "\n", + " valid_price = True\n", + " total += price_product\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid price: {error}\")\n", + " print(\"Please enter a positive numeric value\")\n", + "\n", + " return total" + ] + }, + { + "cell_type": "markdown", + "id": "0cd21824-2368-4661-8a71-098a35d951e1", + "metadata": {}, + "source": [ + "3. Modify the get_customer_orders function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "28c1e666-dd2a-4033-9f2c-46ed8312214a", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = []\n", + "\n", + " valid_number = False\n", + " while valid_number == False:\n", + " try:\n", + " num_orders = int(input(\"How many products do you want to order? \"))\n", + "\n", + " if num_orders <= 0:\n", + " raise ValueError(\"Number of orders must be positive.\")\n", + "\n", + " valid_number = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid number: {error}\")\n", + " print(\"Please enter a positive numeric value.\")\n", + "\n", + " for num in range(num_orders):\n", + " valid_product = False\n", + "\n", + " while valid_product == False:\n", + " try:\n", + " product_name = input(\"Please enter the name of your product: \")\n", + "\n", + " if product_name not in inventory:\n", + " raise ValueError(\"Product not found in inventory.\")\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + "\n", + " valid_product = True\n", + " orders.append(product_name)\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid product: {error}\")\n", + " print(\"Please enter a valid product name.\")\n", + "\n", + " return orders" + ] + }, + { + "cell_type": "markdown", + "id": "bb4729b0-53ab-44f9-b492-cf7ec6ce8a3c", + "metadata": {}, + "source": [ + "4. Test your code by running the program " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c7116dcf-02dc-4be6-a19d-15bb9f9d5df9", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 3\n", + "Please enter the name of your product: t-shirt\n", + "Please enter the name of your product: hut\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product not found in inventory.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: book\n", + "Please enter the name of your product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product is out of stock.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orders: ['t-shirt', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 3\n", + "Enter the price of book: -5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price: Negative prices are not allowed.\n", + "Please enter a positive numeric value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of book: 0\n", + "Enter the price of keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total: 13\n" + ] + } + ], + "source": [ + "# first try\n", + "inventory = {\n", + " \"t-shirt\": 345,\n", + " \"mug\": 0,\n", + " \"hat\": 56,\n", + " \"book\": 9,\n", + " \"keychain\": 1\n", + "}\n", + "\n", + "orders = get_customer_orders(inventory)\n", + "print(\"Orders:\", orders)\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total:\", total)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed0e71de-9de6-46d0-8d80-1ac3f45f75bf", + "metadata": {}, + "outputs": [], + "source": [ + "# above, if the price of book is zero, the program allowed it \n", + "# so i had to change the validation condition (if price_product < 0 to if price_product <= 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "309a531a-4b88-4fd3-a4a7-c9a9c2881e79", + "metadata": {}, + "outputs": [], + "source": [ + "# correção:\n", + "def calculate_total_price(customer_orders):\n", + " total = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_price = False \n", + "\n", + " while valid_price == False:\n", + " try:\n", + " price_product = int(input(f\"Enter the price of {product}: \")) \n", + "\n", + " if price_product <= 0: # blocking zero and negatives\n", + " raise ValueError(\"Zero and Negative prices are not allowed.\")\n", + "\n", + " valid_price = True\n", + " total += price_product\n", + "\n", + " except ValueError as error:\n", + " print(f\"Invalid price: {error}\")\n", + " print(\"Please enter a positive numeric value\")\n", + "\n", + " return total" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0f02ea82-22b0-4a45-965c-5c1cd8c731fc", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "How many products do you want to order? 3\n", + "Please enter the name of your product: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product is out of stock.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: pen\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid product: Product not found in inventory.\n", + "Please enter a valid product name.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Please enter the name of your product: keychain\n", + "Please enter the name of your product: t-shirt\n", + "Please enter the name of your product: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orders: ['keychain', 't-shirt', 'book']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 0\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price: Zero and Negative prices are not allowed.\n", + "Please enter a positive numeric value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: -45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid price: Zero and Negative prices are not allowed.\n", + "Please enter a positive numeric value\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 90\n", + "Enter the price of t-shirt: 5\n", + "Enter the price of book: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total: 119\n" + ] + } + ], + "source": [ + "# second try \n", + "inventory = {\n", + " \"t-shirt\": 345,\n", + " \"mug\": 0,\n", + " \"hat\": 56,\n", + " \"book\": 9,\n", + " \"keychain\": 1\n", + "}\n", + "\n", + "orders = get_customer_orders(inventory)\n", + "print(\"Orders:\", orders)\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total:\", total)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:anaconda3]", "language": "python", - "name": "python3" + "name": "conda-env-anaconda3-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +454,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,