diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c62c5bf 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,159 @@ "\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": "aca453ff", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'sombrero': 5, 'camisetas': 5, 'llavero': 5, 'libro': 5, 'pantalon': 5}" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products=[\"sombrero\",\"camisetas\", \"llavero\", \"libro\", \"pantalon\"]\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} 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", + "inventory= initialize_inventory(products)\n", + "inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e2dcd0eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Producto agregado al pedido.\n", + "Producto agregado al pedido.\n", + "Producto agregado al pedido.\n", + "Tu pedido es: {'sombrero', 'libro', 'camisetas'}\n" + ] + } + ], + "source": [ + "def get_customer_orders():\n", + " \"\"\"\n", + " Función creada para realizar un pedido.\n", + " Primero, solicita la cantidad de productos que el usuario quiere pedir.\n", + " Luego, pide ingresar productos de una lista predefinida y los agrega a un conjunto de pedidos válidos.\n", + " \"\"\"\n", + " \n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Por favor ingrese la cantidad de productos que desea pedir: \"))\n", + " if number_of_orders <= 0:\n", + " print(\"Debe ingresar un número positivo.\")\n", + " else:\n", + " break\n", + " except ValueError:\n", + " print(\"Entrada inválida. Por favor, ingrese un número entero positivo.\")\n", + " \n", + " customer_orders = set()\n", + " while len(customer_orders) < number_of_orders:\n", + " try:\n", + " producto = input(f\"Ingresa el nombre del producto de la siguiente lista {products} para confirmar tu pedido: \")\n", + " \n", + " if producto in inventory:\n", + " customer_orders.add(producto)\n", + " print(\"Producto agregado al pedido.\")\n", + " else:\n", + " print(\"El producto no está en la lista.\")\n", + " except ValueError:\n", + " raise ValueError(\"Entrada no válida. Por favor, ingresa un producto válido.\")\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "customer_orders = get_customer_orders()\n", + "print(\"Tu pedido es:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "301d81f2", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "Error: valor no válido. Por favor, ingrese un número.", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[3], line 14\u001b[0m, in \u001b[0;36mcalculate_total_price\u001b[1;34m(customer_orders)\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m price \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m---> 14\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m (\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: el precio no puede ser negativo. Por favor, ingrese un valor positivo.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 15\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", + "\u001b[1;31mValueError\u001b[0m: Error: el precio no puede ser negativo. Por favor, ingrese un valor positivo.", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[3], line 25\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: valor no válido. Por favor, ingrese un número.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 22\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m total_price\n\u001b[1;32m---> 25\u001b[0m precio_total \u001b[38;5;241m=\u001b[39m \u001b[43mcalculate_total_price\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcustomer_orders\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEl precio total de tu pedido es:\u001b[39m\u001b[38;5;124m\"\u001b[39m, precio_total)\n", + "Cell \u001b[1;32mIn[3], line 20\u001b[0m, in \u001b[0;36mcalculate_total_price\u001b[1;34m(customer_orders)\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m 19\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m:\n\u001b[1;32m---> 20\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: valor no válido. Por favor, ingrese un número.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 22\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m total_price\n", + "\u001b[1;31mValueError\u001b[0m: Error: valor no válido. Por favor, ingrese un número." + ] + } + ], + "source": [ + "def calculate_total_price(customer_orders):\n", + " \"\"\"\n", + " Función que solicita al usuario ingresar el precio de cada producto del pedido.\n", + " Usa como parametro la orden previamente hecha\n", + " Retorna la suma de los precios de la orden y maneja errores de entrada.\n", + " \"\"\"\n", + " total_price = 0.0\n", + " \n", + " for producto in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Ingrese el precio para '{producto}': \"))\n", + " if price < 0:\n", + " raise ValueError (\"Error: el precio no puede ser negativo. Por favor, ingrese un valor positivo.\")\n", + " else:\n", + " total_price += price\n", + " break\n", + "\n", + " except ValueError:\n", + " raise ValueError(\"Error: valor no válido. Por favor, ingrese un número.\")\n", + " \n", + " return total_price\n", + "\n", + "\n", + "precio_total = calculate_total_price(customer_orders)\n", + "print(\"El precio total de tu pedido es:\", precio_total)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +238,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.4" } }, "nbformat": 4,