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
145 changes: 141 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,155 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4e700675",
"metadata": {},
"outputs": [],
"source": [
"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}s 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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b95c616",
"metadata": {},
"outputs": [],
"source": [
"inventory = initialize_inventory(products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cfbd8a4",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" product = input(\"Which product do you want to order? \")\n",
" if product not in inventory:\n",
" print(\"That product does not exist. Try again.\")\n",
" elif inventory[product] == 0:\n",
" print(\"Sorry, that product is out of stock.\")\n",
" else:\n",
" customer_orders.add(product)\n",
" break\n",
"\n",
" \n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d19b76e2",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" try:\n",
" for product in customer_orders:\n",
" if product not in inventory:\n",
" raise KeyError(f\"{product} no existe en el inventario\")\n",
" if inventory[product] <= 0:\n",
" raise ValueError(f\"Sin stock para {product}\")\n",
" inventory[product] -= 1\n",
"\n",
" except KeyError as e:\n",
" print(\"Error de producto:\", e)\n",
" except TypeError:\n",
" print(\"Error: inventory debe ser un diccionario y customer_orders una lista\")\n",
" except ValueError as e:\n",
" print(\"Error de stock:\", e)\n",
" else:\n",
" print(\"Inventario actualizado correctamente\")\n",
" finally:\n",
" print(\"Proceso de actualización finalizado\")\n",
" return inventory\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47ea01b7",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" try:\n",
" total_orders = len(customer_orders)\n",
" total_percentage = (total_orders / len(products)) * 100\n",
" except ZeroDivisionError:\n",
" print(\"Error: la lista de productos está vacía\")\n",
" return total_orders, 0\n",
" except TypeError:\n",
" print(\"Error: los argumentos deben ser listas\")\n",
" else:\n",
" return total_orders, total_percentage\n",
"\n",
"total_orders, total_percentage = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b8e7cfb",
"metadata": {},
"outputs": [],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5eaf9c9",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" total_orders, total_percentage = order_statistics\n",
" print(\"Total orders: \", total_orders)\n",
" print(\"Total percentage of products ordered: \", total_percentage)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +203,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down