Skip to content
Open
98 changes: 96 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,105 @@
"\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": 39,
"id": "464b1353",
"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": null,
"id": "b4154ae6",
"metadata": {},
"outputs": [],
"source": [
"def calculate_total_price(customer_orders): \n",
" total_price = 0.0\n",
" for product in customer_orders:\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" entry = input(f\"Enter the price of {product}: \")\n",
" price = float(entry)\n",
" if price < 0:\n",
" raise ValueError(\"Price cannot be negative. Please enter a valid price.\")\n",
" total_price += price\n",
" valid_input = True\n",
" except ValueError:\n",
" print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n",
" print (f\"Error: '{price}' cannot be negative. Please enter a valid price.\") \n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c568692b",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders(inventory):\n",
" while True:\n",
" try:\n",
" orders_count = int(input(\"Enter the number of customer orders: \"))\n",
" if orders_count < 0:\n",
" raise ValueError(\"The number of orders cannot be negative.\")\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e} Please enter a valid integer.\")\n",
"\n",
" customer_orders = {}\n",
" for i in range(orders_count):\n",
" while True:\n",
" try:\n",
" product_name = input(f\"Gather the name for product {i + 1}: \")\n",
" if product_name not in inventory:\n",
" raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n",
" already_ordered = customer_orders.get(product_name, 0)\n",
" if inventory[product_name] <= already_ordered:\n",
" raise ValueError(f\"'{product_name}' is currently out of stock or insufficient units.\")\n",
" break\n",
" except ValueError as e:\n",
" print(f\"Error: {e} Please try again.\")\n",
"\n",
" return customer_orders\n",
"\n",
" \n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa19394e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +184,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down