Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
316 changes: 314 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,323 @@
"\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": [
{
"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",
" inventory = {}\n",
"\n",
" for product in products:\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",
" inventory[product] = quantity\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return inventory\n",
"\n",
"\n",
"print(\"-------------------------------------------------------------------------------------------\")\n",
"\n",
"\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",
" price = float(input(f\"Enter the price of {product}: \"))\n",
"\n",
" if price < 0:\n",
" raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n",
"\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",
" ]\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": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +402,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down