Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"master"
]
}
167 changes: 163 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,179 @@
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "382b7794",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" \n",
" for product in products:\n",
" while True:\n",
" try: \n",
" cantidad = int(input(f\"Introduce la cantidad de {product}\"))\n",
" if (cantidad > 0):\n",
" inventory[product] = (cantidad)\n",
" print(f\"{product} agregado con éxito\")\n",
" break\n",
" else:\n",
" raise ValueError(\"La cantidad debe ser mayor que 0\")\n",
" except ValueError as e:\n",
" print(f\"Error: {e}. Intenta nuevamente.\")\n",
" finally:\n",
" print(\"Intento de agregar cantidad realizado.\")\n",
" \n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "490be403",
"metadata": {},
"outputs": [],
"source": [
"inventory = {}\n",
"\n",
"def get_customer_orders():\n",
" customer_order = set()\n",
"\n",
" while True:\n",
" try: \n",
" product = input(\"Introduce el nombre del producto que quieres: \").lower() \n",
" if product not in inventory:\n",
" raise ValueError(f\"El producto {product} no se encuentra en el inventario\")\n",
" if inventory[product] <= 0:\n",
" raise ValueError (f\"El producto {product} no tiene stock\")\n",
" customer_order.add(product)\n",
" print (f\"El producto {product} se ha agregado a tu pedido\") \n",
" except ValueError as g:\n",
" print(f\"Error : {g}\")\n",
" \n",
" while True:\n",
" another = input(\"¿Quieres otro producto? (si/no): \").strip().lower()\n",
" if another in [\"si\",\"no\"]:\n",
" break\n",
" else:\n",
" print (\"Por favor, introduce 'si' o 'no'.\")\n",
"\n",
" if another == \"no\":\n",
" break \n",
" return customer_order"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "396611af",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" try:\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" if inventory[product]>0:\n",
" inventory [product]-=1\n",
" else:\n",
" raise ValueError(f\"No hay suficiente stock en el inventario para {product}\")\n",
" except (KeyError, ValueError) as g:\n",
" print(f\"Error: {g}\")\n",
" except TypeError:\n",
" print(\"Error: el inventario o los pedidos no son válidos\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b73cb827",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug sin stock\n",
"(0, 0.0)\n"
]
}
],
"source": [
"products = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"\n",
"customer_order = get_customer_orders()\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" try:\n",
" total_products = len(customer_orders)\n",
" unique_percentage = (total_products/ len(products)) * 100 \n",
" if total_products == 0:\n",
" raise ValueError(\"No se han realizado pedidos\")\n",
" except ZeroDivisionError:\n",
" print(\"No se han introducido productos\")\n",
" total_products = 0\n",
" unique_percentage = 0\n",
" except ValueError as g:\n",
" print (f\"Error: {g}\")\n",
" total_products = 0\n",
" unique_percentage = 0\n",
" return total_products, unique_percentage\n",
"\n",
"print(calculate_order_statistics(customer_order, products))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9b76120",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" try:\n",
" total_products = order_statistics[0]\n",
" unique_percentage = order_statistics[1]\n",
" except (TypeError, IndexError):\n",
" print (\"Error: La estadística del pedido no es correcta\")\n",
" total_products = 0\n",
" unique_percentage = 0\n",
" else:\n",
" print(\"La estadística del pedido se ha realizado con éxito\")\n",
" \n",
" print(f\"Total products ordered: {total_products}\")\n",
" print(f\"Unique products ordered: {unique_percentage}%\")\n",
"\n",
"print_order_statistics(calculate_order_statistics(customer_order, products))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"id": "d1927a4a",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def print_update_inventory(inventory):\n",
" print(\"Inventario actualizado: \")\n",
" try:\n",
" for product, quantity in inventory.items():\n",
" print (f\"{product} : {quantity}\")\n",
" except TypeError:\n",
" print(\"Error: el inventario no es válido\")\n",
" finally:\n",
" print(\"El inventario se ha mostrado correctamente\")\n",
" return inventory"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +225,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down