From 45c28075be71ff6df23b4ad20ebfbf9e725b4fa1 Mon Sep 17 00:00:00 2001 From: eliomartinezcastano-stack Date: Thu, 15 Jan 2026 17:57:13 +0100 Subject: [PATCH] Lab error handling solucionado --- lab-python-error-handling.ipynb | 203 +++++++++++++++++++++++++++++++- 1 file changed, 201 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..a0cbe41 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,210 @@ "\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": 80, + "id": "496e214e", + "metadata": {}, + "outputs": [], + "source": [ + "products =[\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "8d981ba5", + "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\"Introduzca la cantidad {product}s disponibles: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Cantidad invalida! Por favor, introduzca un número que no sea negativo.\")\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": 93, + "id": "c55dbca4", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = {}\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Cuantos productos desea añadir?: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Cantidad invalida! por favor, introduzca un número que no sea negativo\")\n", + " \n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " continue\n", + " \n", + " while True:\n", + " try:\n", + " product_name = input(\"Qué producto desea añadir?: \").strip()\n", + " if product_name not in inventory:\n", + " raise ValueError(\"El producto no está en el inventario.\")\n", + " \n", + " if inventory[product_name] < quantity:\n", + " raise ValueError(\"No hay suficiente stock de este producto.\")\n", + " \n", + " if product_name in customer_orders:\n", + " customer_orders[product_name] += quantity\n", + "\n", + " else:\n", + " customer_orders[product_name] = quantity\n", + "\n", + " inventory[product_name] -= quantity\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error} Introduzca un número válido.\")\n", + " \n", + " add_another = input(\"Desea seguir añadiendo productos? (yes/no)\").strip().lower()\n", + " if add_another != \"yes\":\n", + " break\n", + "\n", + " return customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "daed3668", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " updated_inventory = {product: (cantidad -1 if product in customer_orders else cantidad) for product,cantidad in inventory.items() if (cantidad -1 > 0) or (product not in customer_orders)}\n", + " return updated_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "7a98a0bf", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_unique_products = (total_products_ordered / len(products)) *100\n", + "\n", + " return total_products_ordered, percentage_unique_products" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "8f3f9646", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " \n", + " print (\"order Statistics\")\n", + " print (\"------\")\n", + " print (f\"Total products ordered: {total}\")\n", + " print (f\"Percentage of unique products ordered: {percentage}%\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "id": "8338a514", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated inventory: \")\n", + " [print(f\"{product}: {cantidad}\") for product, cantidad in inventory.items()]" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "5f358a28", + "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\"Introduce el precio de {product}: \"))\n", + " \n", + " if price < 0:\n", + " raise ValueError(\"Precio invalido! Por favor, introduzca un valor no negativo.\")\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " total_price[product] = price\n", + " return total_price\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "6b80b666", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: No hay suficiente stock de este producto.. Introduzca un número válido.\n", + "order Statistics\n", + "------\n", + "Total products ordered: 1\n", + "Percentage of unique products ordered: 20.0%\n", + "Updated inventory: \n", + "t-shirt: 10\n", + "mug: 1\n", + "hat: 7\n", + "book: 10\n", + "keychain: 10\n", + "Total price: 5.0\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "updated_inventory = update_inventory(customer_orders, inventory)\n", + "order_statistics = calculate_order_statistics (customer_orders, products)\n", + "\n", + "print_order_statistics (order_statistics)\n", + "print_updated_inventory (updated_inventory)\n", + "total_price = calculate_total_price (customer_orders)\n", + "print(\"Total price:\", sum(total_price.values()))" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +289,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,