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
177 changes: 175 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,184 @@
"\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": "ff8cc87d",
"metadata": {},
"outputs": [],
"source": [
"# PASO 1: initialize_inventory() con Dictionary Comprehension\n",
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the available quantity for {product}: \")) for product in products}\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af5a99ff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n",
"Error: invalid literal for int() with base 10: ''\n"
]
}
],
"source": [
"# PASO 1: initialize_inventory() adaptado para que funcione con validación\n",
"def initialize_inventory(products):\n",
" inventory = {}\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 available quantity for {product}: \"))\n",
"\n",
" if quantity < 0:\n",
" raise ValueError(\"Quantity cannot be negative.\")\n",
"\n",
" inventory[product] = quantity\n",
" valid_quantity = True\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return inventory\n",
"\n",
"\n",
"# PASO 2: get_customer_orders() con validación de número de pedidos y producto\n",
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
"\n",
" valid_orders_number = False\n",
" while not valid_orders_number:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
"\n",
" if num_orders < 0:\n",
" raise ValueError(\"Number of orders cannot be negative.\")\n",
"\n",
" valid_orders_number = True\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" for i in range(num_orders):\n",
" valid_product = False\n",
"\n",
" while not valid_product:\n",
" try:\n",
" product = input(f\"Enter product name {i + 1}: \").strip()\n",
"\n",
" if product not in inventory:\n",
" raise ValueError(\"This product is not in the inventory.\")\n",
"\n",
" if inventory[product] <= 0:\n",
" raise ValueError(\"This product is out of stock.\")\n",
"\n",
" customer_orders.add(product)\n",
" valid_product = True\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"# PASO 3: calculate_total_price() con validación del precio\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
"\n",
" for product in customer_orders:\n",
" valid_price = False\n",
"\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
"\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
"\n",
" total_price += price\n",
" valid_price = True\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
"\n",
" return total_price\n",
"\n",
"\n",
"# PASO 4: update_inventory() para restar stock y eliminar productos con 0\n",
"def update_inventory(customer_orders, inventory):\n",
" updated_inventory = {}\n",
"\n",
" for product, quantity in inventory.items():\n",
" if product in customer_orders:\n",
" quantity -= 1\n",
"\n",
" if quantity > 0:\n",
" updated_inventory[product] = quantity\n",
"\n",
" return updated_inventory\n",
"\n",
"\n",
"# PASO 5: calculate_order_statistics() para calcular estadísticas del pedido\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" order_statistics = (total_products_ordered, percentage_ordered)\n",
" return order_statistics\n",
"\n",
"\n",
"# PASO 6: print_order_statistics() para imprimir estadísticas\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total products ordered: {order_statistics[0]}\")\n",
" print(f\"Percentage of unique products ordered: {order_statistics[1]}%\")\n",
"\n",
"\n",
"# PASO 7: print_updated_inventory() para imprimir inventario actualizado\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"# PASO 8: ejecución principal del programa\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(inventory)\n",
"total_price = calculate_total_price(customer_orders)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print(f\"Total price of the customer order: {total_price}\")\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +263,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down