From ac115029c57403ea2cf93590729a098654879368 Mon Sep 17 00:00:00 2001 From: "ERIKA J. DIAZ DAFT ABR2026" Date: Tue, 21 Apr 2026 22:47:58 +0200 Subject: [PATCH 1/3] Create lab-python-error-handling ejercicio.ipynb --- lab-python-error-handling ejercicio.ipynb | 290 ++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 lab-python-error-handling ejercicio.ipynb diff --git a/lab-python-error-handling ejercicio.ipynb b/lab-python-error-handling ejercicio.ipynb new file mode 100644 index 0000000..7907b55 --- /dev/null +++ b/lab-python-error-handling ejercicio.ipynb @@ -0,0 +1,290 @@ +{ + "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": "09d5632c", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "19428dbb", + "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(\"Quantity cannot be negative.\")\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": 3, + "id": "471b263b", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = set()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3382fe1f", + "metadata": {}, + "outputs": [ + { + "ename": "IndentationError", + "evalue": "unexpected indent (2019598857.py, line 2)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mwhile not valid_number:\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m unexpected indent\n" + ] + } + ], + "source": [ + "valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders <= 0:\n", + " raise ValueError(\"Number of orders must be greater than zero.\")\n", + " valid_number = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "baffcb40", + "metadata": {}, + "outputs": [ + { + "ename": "IndentationError", + "evalue": "unindent does not match any outer indentation level (, line 15)", + "output_type": "error", + "traceback": [ + " \u001b[36mFile \u001b[39m\u001b[32m:15\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mreturn customer_orders\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m unindent does not match any outer indentation level\n" + ] + } + ], + "source": [ + "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: \").strip().lower()\n", + " if product not in inventory:\n", + " raise ValueError(f\"'{product}' is not in the inventory. Available: {list(inventory.keys())}\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(f\"'{product}' is out of stock.\")\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " customer_orders.add(product)\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "fb914c8a", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = []\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", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " total_price.append((product, price))\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "12db293d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory: {'t-shirt': 9, 'mug': 8, 'hat': 7, 'book': 6, 'keychain': 5}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(\"\\nInventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "d2d584bd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer Orders: None\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(inventory)\n", + "print(\"\\nCustomer Orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "2e9a00eb", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'NoneType' object is not iterable", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[10]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m prices = \u001b[43mcalculate_total_price\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 2\u001b[39m total = \u001b[38;5;28msum\u001b[39m(price \u001b[38;5;28;01mfor\u001b[39;00m _, price \u001b[38;5;129;01min\u001b[39;00m prices)\n\u001b[32m 3\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33mTotal Price: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtotal\u001b[38;5;132;01m:\u001b[39;00m\u001b[33m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m€\u001b[39m\u001b[33m\"\u001b[39m)\n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[7]\u001b[39m\u001b[32m, line 3\u001b[39m, in \u001b[36mcalculate_total_price\u001b[39m\u001b[34m(customer_orders)\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mcalculate_total_price\u001b[39m(customer_orders):\n\u001b[32m 2\u001b[39m total_price = []\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mproduct\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m:\u001b[49m\n\u001b[32m 4\u001b[39m \u001b[43m \u001b[49m\u001b[43mvalid_price\u001b[49m\u001b[43m \u001b[49m\u001b[43m=\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\n\u001b[32m 5\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mwhile\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mvalid_price\u001b[49m\u001b[43m:\u001b[49m\n", + "\u001b[31mTypeError\u001b[39m: 'NoneType' object is not iterable" + ] + } + ], + "source": [ + "prices = calculate_total_price(customer_orders)\n", + "total = sum(price for _, price in prices)\n", + "print(f\"\\nTotal Price: {total:.2f}€\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 020c1818e6521742a43bc661662d2e1b1c09c074 Mon Sep 17 00:00:00 2001 From: "ERIKA J. DIAZ DAFT ABR2026" Date: Wed, 29 Apr 2026 19:01:52 +0200 Subject: [PATCH 2/3] =?UTF-8?q?A=C3=B1adir=20ejercicio=20completado?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab-python-error-handling ejercicio.ipynb | 172 +++++----------------- 1 file changed, 38 insertions(+), 134 deletions(-) diff --git a/lab-python-error-handling ejercicio.ipynb b/lab-python-error-handling ejercicio.ipynb index 7907b55..753e188 100644 --- a/lab-python-error-handling ejercicio.ipynb +++ b/lab-python-error-handling ejercicio.ipynb @@ -73,23 +73,36 @@ "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": "09d5632c", - "metadata": {}, - "outputs": [], - "source": [ - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" - ] - }, { "cell_type": "code", "execution_count": 2, - "id": "19428dbb", + "id": "09d5632c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'hola'\n", + "Error: invalid literal for int() with base 10: 'bueno'\n", + "\n", + "Inventory: {'t-shirt': 7, 'mug': 9, 'hat': 6, 'book': 4, 'keychain': 6}\n", + "Error: invalid literal for int() with base 10: 'abc'\n", + "Error: Number of orders must be greater than zero.\n", + "Error: 'pizza' is not in the inventory. Available: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "Error: 'chain' is not in the inventory. Available: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "Customer Orders: {'keychain', 'hat'}\n", + "Error: Price cannot be negative.\n", + "Error: could not convert string to float: \"'30\"\n", + "\n", + "Total Price: 13.90€\n" + ] + } + ], "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -103,37 +116,10 @@ " except ValueError as error:\n", " print(f\"Error: {error}\")\n", " inventory[product] = quantity\n", - " return inventory" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "471b263b", - "metadata": {}, - "outputs": [], - "source": [ + " return inventory\n", "def get_customer_orders(inventory):\n", - " customer_orders = set()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "3382fe1f", - "metadata": {}, - "outputs": [ - { - "ename": "IndentationError", - "evalue": "unexpected indent (2019598857.py, line 2)", - "output_type": "error", - "traceback": [ - " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mwhile not valid_number:\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m unexpected indent\n" - ] - } - ], - "source": [ - "valid_number = False\n", + " customer_orders = set()\n", + " valid_number = False\n", " while not valid_number:\n", " try:\n", " num_orders = int(input(\"Enter the number of customer orders: \"))\n", @@ -141,26 +127,8 @@ " raise ValueError(\"Number of orders must be greater than zero.\")\n", " valid_number = True\n", " except ValueError as error:\n", - " print(f\"Error: {error}\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "baffcb40", - "metadata": {}, - "outputs": [ - { - "ename": "IndentationError", - "evalue": "unindent does not match any outer indentation level (, line 15)", - "output_type": "error", - "traceback": [ - " \u001b[36mFile \u001b[39m\u001b[32m:15\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mreturn customer_orders\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m unindent does not match any outer indentation level\n" - ] - } - ], - "source": [ - "for _ in range(num_orders):\n", + " print(f\"Error: {error}\")\n", + " for _ in range(num_orders):\n", " valid_product = False\n", " while not valid_product:\n", " try:\n", @@ -174,16 +142,7 @@ " print(f\"Error: {error}\")\n", " customer_orders.add(product)\n", "\n", - " return customer_orders" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "fb914c8a", - "metadata": {}, - "outputs": [], - "source": [ + " return customer_orders\n", "def calculate_total_price(customer_orders):\n", " total_price = []\n", " for product in customer_orders:\n", @@ -197,72 +156,17 @@ " except ValueError as error:\n", " print(f\"Error: {error}\")\n", " total_price.append((product, price))\n", - " return total_price" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "12db293d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Inventory: {'t-shirt': 9, 'mug': 8, 'hat': 7, 'book': 6, 'keychain': 5}\n" - ] - } - ], - "source": [ + " return total_price\n", + "\n", "inventory = initialize_inventory(products)\n", - "print(\"\\nInventory:\", inventory)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "d2d584bd", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Customer Orders: None\n" - ] - } - ], - "source": [ + "print(\"\\nInventory:\", inventory)\n", + "\n", "customer_orders = get_customer_orders(inventory)\n", - "print(\"\\nCustomer Orders:\", customer_orders)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "2e9a00eb", - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'NoneType' object is not iterable", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[10]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m prices = \u001b[43mcalculate_total_price\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 2\u001b[39m total = \u001b[38;5;28msum\u001b[39m(price \u001b[38;5;28;01mfor\u001b[39;00m _, price \u001b[38;5;129;01min\u001b[39;00m prices)\n\u001b[32m 3\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33mTotal Price: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtotal\u001b[38;5;132;01m:\u001b[39;00m\u001b[33m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m€\u001b[39m\u001b[33m\"\u001b[39m)\n", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[7]\u001b[39m\u001b[32m, line 3\u001b[39m, in \u001b[36mcalculate_total_price\u001b[39m\u001b[34m(customer_orders)\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mcalculate_total_price\u001b[39m(customer_orders):\n\u001b[32m 2\u001b[39m total_price = []\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mproduct\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m:\u001b[49m\n\u001b[32m 4\u001b[39m \u001b[43m \u001b[49m\u001b[43mvalid_price\u001b[49m\u001b[43m \u001b[49m\u001b[43m=\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\n\u001b[32m 5\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mwhile\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mvalid_price\u001b[49m\u001b[43m:\u001b[49m\n", - "\u001b[31mTypeError\u001b[39m: 'NoneType' object is not iterable" - ] - } - ], - "source": [ + "print(\"\\nCustomer Orders:\", customer_orders)\n", + "\n", "prices = calculate_total_price(customer_orders)\n", "total = sum(price for _, price in prices)\n", - "print(f\"\\nTotal Price: {total:.2f}€\")" + "print(f\"\\nTotal Price: {total:.2f}€\")\n" ] } ], From 1136150a0fe237c9ac41826c8cd4b7fa48ad3036 Mon Sep 17 00:00:00 2001 From: "ERIKA J. DIAZ DAFT ABR2026" Date: Wed, 29 Apr 2026 19:22:55 +0200 Subject: [PATCH 3/3] Update lab-python-error-handling ejercicio.ipynb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit falta de datos actualización --- lab-python-error-handling ejercicio.ipynb | 41 +++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/lab-python-error-handling ejercicio.ipynb b/lab-python-error-handling ejercicio.ipynb index 753e188..c5b19f6 100644 --- a/lab-python-error-handling ejercicio.ipynb +++ b/lab-python-error-handling ejercicio.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "09d5632c", "metadata": {}, "outputs": [ @@ -84,25 +84,24 @@ "output_type": "stream", "text": [ "Error: invalid literal for int() with base 10: 'hola'\n", - "Error: invalid literal for int() with base 10: 'bueno'\n", - "\n", - "Inventory: {'t-shirt': 7, 'mug': 9, 'hat': 6, 'book': 4, 'keychain': 6}\n", "Error: invalid literal for int() with base 10: 'abc'\n", - "Error: Number of orders must be greater than zero.\n", - "Error: 'pizza' is not in the inventory. Available: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "Error: 'chain' is not in the inventory. Available: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", "\n", - "Customer Orders: {'keychain', 'hat'}\n", - "Error: Price cannot be negative.\n", - "Error: could not convert string to float: \"'30\"\n", + "Inventory: {'t-shirt': 9, 'mug': 0, 'hat': 8, 'book': 5, 'keychain': 6}\n", + "Error: invalid literal for int() with base 10: 'hola'\n", + "Error: 'mug' is out of stock.\n", + "\n", + "Customer Orders: {'book', 'hat'}\n", + "Error: could not convert string to float: 'hgi'\n", "\n", - "Total Price: 13.90€\n" + "Total Price: 23.90€\n" ] } ], "source": [ + "#Paso 1\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", + "#Paso 2\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -117,14 +116,16 @@ " print(f\"Error: {error}\")\n", " inventory[product] = quantity\n", " return inventory\n", + "\n", + "#Paso 3\n", "def get_customer_orders(inventory):\n", " customer_orders = set()\n", " valid_number = False\n", " while not valid_number:\n", " try:\n", " num_orders = int(input(\"Enter the number of customer orders: \"))\n", - " if num_orders <= 0:\n", - " raise ValueError(\"Number of orders must be greater than zero.\")\n", + " if num_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", " valid_number = True\n", " except ValueError as error:\n", " print(f\"Error: {error}\")\n", @@ -135,14 +136,15 @@ " product = input(\"Enter the name of a product: \").strip().lower()\n", " if product not in inventory:\n", " raise ValueError(f\"'{product}' is not in the inventory. Available: {list(inventory.keys())}\")\n", - " if inventory[product] == 0:\n", + " if inventory[product] == 0: #Para fuera de stock\n", " raise ValueError(f\"'{product}' is out of stock.\")\n", " valid_product = True\n", " except ValueError as error:\n", " print(f\"Error: {error}\")\n", " customer_orders.add(product)\n", - "\n", " return customer_orders\n", + "\n", + "#Paso 4\n", "def calculate_total_price(customer_orders):\n", " total_price = []\n", " for product in customer_orders:\n", @@ -158,13 +160,16 @@ " total_price.append((product, price))\n", " return total_price\n", "\n", - "inventory = initialize_inventory(products)\n", + "#Paso 4\n", + "inventory = initialize_inventory(products) #evitar entrada de string o cantidad negativa\n", "print(\"\\nInventory:\", inventory)\n", "\n", - "customer_orders = get_customer_orders(inventory)\n", + "#Paso 5\n", + "customer_orders = get_customer_orders(inventory) # Para fuera de stock,inexistente o pedidos negativos\n", "print(\"\\nCustomer Orders:\", customer_orders)\n", "\n", - "prices = calculate_total_price(customer_orders)\n", + "#Paso 6\n", + "prices = calculate_total_price(customer_orders)# Para precios negativos o entrada de string en vez de float\n", "total = sum(price for _, price in prices)\n", "print(f\"\\nTotal Price: {total:.2f}€\")\n" ]