diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..79ffadb 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,99 @@ "\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": 1, + "id": "039285aa-17bb-45ca-a44e-a64204ce2b9e", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initialize inventory with error handling\n", + "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", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "eac682a1-65cc-4b94-8a1a-0ae27f3aef11", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Calculate total price with error handling\n", + "def calculate_total_price(product, inventory):\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " quantity = inventory[product]\n", + " return price * quantity\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "77b6f687-13df-4b64-b88c-0874a4cf7a8c", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 3: Get customer orders with error handling\n", + "def get_customer_orders(inventory):\n", + " orders = {}\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of orders: \"))\n", + " if num_orders <= 0:\n", + " raise ValueError(\"Number of orders must be positive.\")\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " for i in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product_name = input(f\"Enter product name for order {i+1}: \")\n", + " if product_name not in inventory:\n", + " print(\"Error: Product not in inventory. Try again.\")\n", + " elif inventory[product_name] == 0:\n", + " print(\"Error: Product out of stock. Try again.\")\n", + " else:\n", + " valid_product = True\n", + " orders[product_name] = orders.get(product_name, 0) + 1\n", + " inventory[product_name] -= 1\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2189fbd-b5ec-4ed9-be18-96e734517be9", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +176,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,