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
132 changes: 130 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,139 @@
"\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": "markdown",
"id": "0c33cc12",
"metadata": {},
"source": [
"Funcion: def initialize_inventory(products): - recibe una lista de productos y devuelve un diccionario con cantidades. \n",
"1. Crear diccionario vacio\n",
"2. Recorrer productos\n",
"3. Bucle infinito (while True) - no salgo de aqui hasta que el usuario lo haga bien\n",
"4. Intentar convertir en numero (try) - aqui puede fallar si el usuario escribe 'hola' (valueError) o '5.5' \n",
"5. Validacion manual:\n",
"if quantity < 0:\n",
" print('Must be >=0') esto lo ponemos porque no queremos valores negativos. \n",
"6. \n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "054fb350",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_quantity = False\n",
" while not valid_quantity:\n",
" try:\n",
" quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n",
" if quantity < 0:\n",
" raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n",
" valid_quantity = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" inventory[product] = quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "5957c81e",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
"\n",
" for product in customer_orders:\n",
" while True:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
"\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative.\")\n",
"\n",
" total_price += price\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}. Please enter a valid price.\")\n",
"\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "112e97f7",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" try:\n",
" product = input(\"Enter the name of a product: \").lower()\n",
"\n",
" if product not in inventory:\n",
" raise ValueError(\"Product does not exist in inventory.\")\n",
"\n",
" if inventory[product] <= 0:\n",
" raise ValueError(\"Product is out of stock.\")\n",
"\n",
" customer_orders.add(product)\n",
" another_order = input(\"Do you want to add another product? (yes/no): \").lower()\n",
"\n",
" if another_order == \"no\":\n",
" break\n",
"\n",
" except ValueError as error:\n",
" print(f\"Error: {error}\")\n",
" \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f1b35e06",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory: {'t-shirt': 5, 'mug': 4, 'hat': 8, 'book': 3, 'keychain': 9}\n",
"Customer orders: {'mug'}\n",
"Total price: 3.0\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = initialize_inventory(products)\n",
"\n",
"customer_orders = get_customer_orders(inventory)\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"\n",
"print(\"Inventory:\", inventory)\n",
"print(\"Customer orders:\", customer_orders)\n",
"print(\"Total price:\", total_price)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +218,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down