diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..1fdda1a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,305 @@ "\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": "449644e8", + "metadata": {}, + "source": [ + "1. 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." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e1925ab1", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3f6ebb2b", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"pen\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6892227c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: ' '\n", + "Error: invalid literal for int() with base 10: 'k'\n", + "Error: invalid literal for int() with base 10: '1.6'\n", + "{'t-shirt': 5, 'mug': 2, 'hat': 1, 'book': 10, 'pen': 6}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "markdown", + "id": "e2626f31", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e2338710", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " num_orders = int(input(\"Enter the number of customer orders \"))\n", + " customer_orders = [input(\"Enter a product to order: \") for product in range(num_orders)]\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "7cef5460", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['mug', 'hat']\n" + ] + } + ], + "source": [ + "orders = get_customer_orders()\n", + "print(orders)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7ddaa07e", + "metadata": {}, + "outputs": [], + "source": [ + "prices = [] #siempre poner las listas, dict, etc, FUERA de la función" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6247f42", + "metadata": {}, + "outputs": [], + "source": [ + "def total_price(orders):\n", + "\n", + " \n", + " for product in orders:\n", + " valid_price = False\n", + " \n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of the {product}: \"))\n", + "\n", + " if price <= 0:\n", + " raise ValueError(\"The price cannot be negative or zero\")\n", + "\n", + " valid_price = True\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}. Please, enter a valid price\")\n", + "\n", + " prices.append(price)\n", + " \n", + " return sum(prices)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "b3707237", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11.0\n" + ] + } + ], + "source": [ + "prices = total_price(orders)\n", + "print(prices)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "a7b0a362", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: The price cannot be negative or zero. Please, enter a valid price\n", + "Error: The price cannot be negative or zero. Please, enter a valid price\n", + "Error: could not convert string to float: 'l'. Please, enter a valid price\n", + "Error: could not convert string to float: ' '. Please, enter a valid price\n", + "Error: could not convert string to float: ' '. Please, enter a valid price\n", + "Error: The price cannot be negative or zero. Please, enter a valid price\n", + "7.0\n" + ] + } + ], + "source": [ + "prices = total_price(orders)\n", + "print(prices)" + ] + }, + { + "cell_type": "markdown", + "id": "954de579", + "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": 43, + "id": "4ca0339c", + "metadata": {}, + "outputs": [], + "source": [ + " customer_orders = []" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "0f58964c", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " \n", + " valid_number = True\n", + " while valid_number:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders \"))\n", + " if num_orders <= 0:\n", + " raise ValueError(\"The number of customer orders cannot be negative or 0\")\n", + " valid_number = False\n", + " except ValueError as error:\n", + " print(f\"Error: {error}. Please enter a valid number of customer orders\")\n", + "\n", + "\n", + "\n", + " for i in range(num_orders):\n", + " valid_product = True\n", + "\n", + " while valid_product:\n", + " try: \n", + " product = input(\"Enter a product to order:\")\n", + " if product not in inventory: \n", + " raise ValueError(\"This product is not in the inventory\")\n", + " if inventory[product]<= 0:\n", + " raise ValueError(\"This product doesn't have stock available\")\n", + " valid_product = False \n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}. Please enter a valid product.\")\n", + " \n", + " customer_orders.append(product)\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "d97b76e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['hat', 'book']\n" + ] + } + ], + "source": [ + "orders = get_customer_orders()\n", + "print(orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "11c86164", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['hat', 'book']\n" + ] + } + ], + "source": [ + "print(customer_orders)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +384,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,