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
69 changes: 67 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
" print(f\"Error: {error}\")\n",
" inventory[product] = quantity\n",
" return inventory\n",
" \n",
" \n",
"\n",
"# Or, in another way:\n",
"\n",
Expand Down Expand Up @@ -64,6 +66,9 @@
"2. Modify the `calculate_total_price` function to include error handling.\n",
" - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n",
" - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n",
" \n",
"\n",
" \n",
"\n",
"3. Modify the `get_customer_orders` function to include error handling.\n",
" - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n",
Expand All @@ -72,11 +77,71 @@
"\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": 3,
"id": "4292b28c",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders):\n",
" total_price = 2\n",
" for product in customer_orders:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
" if price < 2:\n",
" raise ValueError(\"Price cannot be less than 2.\")\n",
" total_price += price\n",
" valid_price = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error} Please enter a valid price.\")\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "feb77992",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" # Validar número de pedidos\n",
" valid_num = False\n",
" while not valid_num:\n",
" try:\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" if num_orders <= 0:\n",
" raise ValueError(\"Number of orders must be positive.\")\n",
" valid_num = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error} Please enter a valid number.\")\n",
"\n",
" customer_orders = set()\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" try:\n",
" product = input(\"Enter the name of a product: \").strip()\n",
" if product not in inventory:\n",
" raise ValueError(f\"'{product}' is not in the inventory.\")\n",
" if inventory[product] == 0:\n",
" raise ValueError(f\"'{product}' is out of stock.\")\n",
" customer_orders.add(product)\n",
" valid_product = True\n",
" except ValueError as error:\n",
" print(f\"Error: {error} Please enter a valid product.\")\n",
"\n",
" return customer_orders"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv (3.15.0)",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +155,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.15.0a7"
}
},
"nbformat": 4,
Expand Down