From ec11b83c9a727c91cbe2e9006906903f2e9bb8d2 Mon Sep 17 00:00:00 2001 From: demonvaa Date: Tue, 21 Apr 2026 16:14:33 +0200 Subject: [PATCH 1/2] Revision 21042026 --- lab-python-error-handling.ipynb | 148 ++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..832f64b 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,154 @@ "\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": null, + "id": "921641c8", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " cantidades = [] \n", + "\n", + " for product in products:\n", + " valid_quantity = False\n", + "\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + "\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + "\n", + " valid_quantity = True\n", + " cantidades.append(quantity) # ← guardamos la cantidad\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " inventory = {producto: cantidad for producto, cantidad in zip(products, cantidades)} #tengo 2 listas para crear el diccionario\n", + " return inventory\n", + "\n", + "print(\"-------------------------------------------------------------------------------------------\")\n", + "\n", + "def get_customer_orders(product_list):\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " for product in product_list:\n", + " valid_quantity = False\n", + "\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the number of customer orders: \"))\n", + "\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + "\n", + " valid_quantity = True\n", + " cantidades.append(quantity) # ← guardamos la cantidad\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " orders = [\n", + " input(\"Enter the name of a product that a customer wants to order: \")\n", + " for _ in range(number_of_orders)\n", + " ]\n", + "\n", + " # Filtrar válidos y eliminar duplicados manteniendo orden\n", + " valid_orders = []\n", + " for item in orders:\n", + " if item in product_list and item not in valid_orders:\n", + " valid_orders.append(item)\n", + " \n", + " return valid_orders\n", + "\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " # 1. Restar 1 a los productos pedidos\n", + " updated_quantities = {\n", + " product: (quantity - 1 if product in customer_orders else quantity)\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + " # 2. Filtrar productos agotados\n", + " cleaned_inventory = {\n", + " product: quantity\n", + " for product, quantity in updated_quantities.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " return cleaned_inventory\n", + "\n", + "\n", + "\n", + "def print_updated_inventory(updated_inventory): # parametro de entrada diccionario\n", + " print(\"\\nUpdated Inventory:\") # lo pongo para cumplir con el titulo\n", + " for product, quantity in updated_inventory.items():\n", + " print(f\"{product}: {quantity}\") # salida IMPRESION\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, product_list): # parametros de entrada una lista y un diccionario\n", + " total = len(customer_orders)\n", + " percentage = (total / len(product_list)) * 100\n", + " return total, percentage # devuelve un entero y un porcentaja para estadistica\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(f\"Order Statistics: \")\n", + " print(f\"Total Products Ordered: {total}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage}\")\n", + "\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = {\n", + " product: float(input(f\"Enter the price of {product}: \"))\n", + " for product in customer_orders\n", + " }\n", + "\n", + " total = sum(prices.values())\n", + "\n", + " return total\n", + "\n", + "\n", + "def print_total_price(total):\n", + " print(f\"Total Price: {total}\")\n", + "\n", + "\n", + "# -------------------------\n", + "# PROGRAMA PRINCIPAL\n", + "# -------------------------\n", + "\n", + "lista_productos = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "inventory = initialize_inventory(lista_productos) # parametro es una lista\n", + " # devuelve un diccionario \n", + "\n", + "customer_orders = get_customer_orders(lista_productos) # parametro es una lista\n", + " # devuelve una lista \n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, lista_productos) # parametros entrada una lista y un diccionario,\n", + " # devuelve un entero y un porcentaja para estadistica\n", + "\n", + "print_order_statistics(order_statistics) # parametros entrada datos de estadistica\n", + " # salida IMPRESION \n", + "\n", + "inventory = update_inventory(customer_orders, inventory) # parametro de entrada una lista y un diccionario\n", + " # devuelve un diccionario\n", + "\n", + "print_updated_inventory(inventory) # parametro de entrada diccionario\n", + " # salida IMPRESION\n", + "\n", + "total = calculate_total_price(customer_orders) # parametro entrada lista\n", + " # salida datos para estatidistica\n", + "\n", + "print_total_price( total) # parametros entrada datos estadistica\n", + " # salida IMPRESION" + ] } ], "metadata": { From b77ed00b24613f2ddaf794e0a695d58b38087ac3 Mon Sep 17 00:00:00 2001 From: demonvaa Date: Fri, 8 May 2026 12:23:50 +0200 Subject: [PATCH 2/2] Revision 08/05-2026 --- lab-python-error-handling.ipynb | 204 ++++++++++++++++++++++++++++---- 1 file changed, 184 insertions(+), 20 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 832f64b..1341145 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -78,50 +78,214 @@ "execution_count": null, "id": "921641c8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-------------------------------------------------------------------------------------------\n", + "Error: invalid literal for int() with base 10: 'o'\n", + "Error: invalid literal for int() with base 10: 'o'\n", + "Error: invalid literal for int() with base 10: '-o'\n", + "Error: invalid literal for int() with base 10: 'o'\n", + "Error: invalid literal for int() with base 10: 'p'\n", + "Error: Product not found in inventory.\n", + "Order Statistics: \n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 40\n", + "mug: 50\n", + "hat: 59\n", + "book: 70\n", + "keychain: 79\n", + "Total Price: 15.0\n" + ] + } + ], "source": [ "def initialize_inventory(products):\n", - " cantidades = [] \n", + " inventory = {}\n", "\n", " for product in products:\n", - " valid_quantity = False\n", - "\n", - " while not valid_quantity:\n", + " while True:\n", " try:\n", " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", "\n", " if quantity < 0:\n", " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", "\n", - " valid_quantity = True\n", - " cantidades.append(quantity) # ← guardamos la cantidad\n", + " inventory[product] = quantity\n", + " break\n", "\n", " except ValueError as error:\n", " print(f\"Error: {error}\")\n", "\n", - " inventory = {producto: cantidad for producto, cantidad in zip(products, cantidades)} #tengo 2 listas para crear el diccionario\n", " return inventory\n", "\n", + "\n", "print(\"-------------------------------------------------------------------------------------------\")\n", "\n", - "def get_customer_orders(product_list):\n", - " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", - " for product in product_list:\n", - " valid_quantity = False\n", "\n", - " while not valid_quantity:\n", + "def ask_number_of_orders():\n", + " while True:\n", + " try:\n", + " number = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if number < 0:\n", + " raise ValueError(\"Invalid number! Please enter a non-negative value.\")\n", + "\n", + " return number\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "\n", + " return number_of_orders\n", + "print(\"-------------------------------------------------------------------------------------------\")\n", + " \n", + "def ask_valid_products(number_of_orders, product_list, inventory):\n", + " orders = []\n", + "\n", + " while len(orders) < number_of_orders:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + "\n", + " if product not in product_list:\n", + " print(\"Error: Product not found in inventory.\")\n", + " continue\n", + "\n", + " if inventory[product] == 0:\n", + " print(\"Error: Product is out of stock.\")\n", + " continue\n", + "\n", + " orders.append(product)\n", + "\n", + " return orders\n", + "\n", + "print(\"-------------------------------------------------------------------------------------------\")\n", + "\n", + "def get_customer_orders(product_list, inventory):\n", + " number_of_orders = ask_number_of_orders()\n", + " orders = ask_valid_products(number_of_orders, product_list, inventory)\n", + "\n", + " seen = set()\n", + " unique_orders = [item for item in orders if not (item in seen or seen.add(item))]\n", + "\n", + " return unique_orders\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory): # 1. Restar 1 a los productos pedidos por que lo pide asi, no te compliques\n", + "\n", + " updated_quantities = {\n", + " product: (quantity - 1 if product in customer_orders else quantity)\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + " cleaned_inventory = {\n", + " product: quantity\n", + " for product, quantity in updated_quantities.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " return cleaned_inventory\n", + "\n", + "\n", + "\n", + "def print_updated_inventory(updated_inventory): # parametro de entrada diccionario\n", + " print(\"\\nUpdated Inventory:\") # lo pongo para cumplir con el titulo\n", + " for product, quantity in updated_inventory.items():\n", + " print(f\"{product}: {quantity}\") # salida IMPRESION\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, product_list): # parametros de entrada una lista y un diccionario\n", + " total = len(customer_orders)\n", + " percentage = (total / len(product_list)) * 100\n", + " return total, percentage # devuelve un entero y un porcentaja para estadistica\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(f\"Order Statistics: \")\n", + " print(f\"Total Products Ordered: {total}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage}\")\n", + "\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total = 0.0\n", + "\n", + " for product in customer_orders:\n", + " while True:\n", " try:\n", - " quantity = int(input(f\"Enter the number of customer orders: \"))\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", "\n", - " if quantity < 0:\n", - " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", "\n", - " valid_quantity = True\n", - " cantidades.append(quantity) # ← guardamos la cantidad\n", + " total += price\n", + " break\n", "\n", " except ValueError as error:\n", " print(f\"Error: {error}\")\n", "\n", + " return total\n", + "\n", + "\n", + "\n", + "\n", + "def print_total_price(total):\n", + " print(f\"Total Price: {total}\")\n", + "\n", + "\n", + "# -------------------------\n", + "# PROGRAMA PRINCIPAL\n", + "# -------------------------\n", + "\n", + "lista_productos = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "inventory = initialize_inventory(lista_productos) # parametro es una lista\n", + " # devuelve un diccionario \n", + "\n", + "\n", + "customer_orders = get_customer_orders(lista_productos, inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, lista_productos) # parametros entrada una lista y un diccionario,\n", + " # devuelve un entero y un porcentaja para estadistica\n", + "\n", + "print_order_statistics(order_statistics) # parametros entrada datos de estadistica\n", + " # salida IMPRESION \n", + "\n", + "inventory = update_inventory(customer_orders, inventory) # parametro de entrada una lista y un diccionario\n", + " # devuelve un diccionario\n", + "\n", + "print_updated_inventory(inventory) # parametro de entrada diccionario\n", + " # salida IMPRESION\n", + "\n", + "total = calculate_total_price(customer_orders) # parametro entrada lista\n", + " # salida datos para estatidistica\n", + "\n", + "print_total_price( total) # parametros entrada datos estadistica\n", + " # salida IMPRESION" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a95b565f", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products): # parametro de entrada una lista\n", + " inventory = {\n", + " producto: int(input(f\"Enter the quantity of {producto} available: \"))\n", + " for producto in products\n", + " }\n", + " return inventory # devuelve un inventario\n", + "\n", + "def get_customer_orders(product_list):\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", " orders = [\n", " input(\"Enter the name of a product that a customer wants to order: \")\n", " for _ in range(number_of_orders)\n", @@ -224,7 +388,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -238,7 +402,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,