From 696178b82545f2b97f06066468614eb1d2d10723 Mon Sep 17 00:00:00 2001 From: Mandana Soltani Date: Tue, 24 Mar 2026 13:09:30 +0100 Subject: [PATCH] Add error handling updates to lab notebook --- lab-python-error-handling.ipynb | 92 ++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..fe74e85 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,96 @@ "\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": 16, + "id": "29ea4792-f07a-4c60-ab56-73ada714453d", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " prices = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price for {product}: \"))\n", + " if price > 0:\n", + " prices[product] = price \n", + " valid_input - True\n", + " else:\n", + " print(\"Price cant be negative. Please enter a valid number.\")\n", + " except ValueEror:\n", + " print(\"Invalid input. Please enter a numeric price.\")\n", + " return prices \n", + "\n", + " \n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "2abfdb53-992c-41bd-b945-bcfd00a5a90e", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customers_orders(inventory):\n", + " orders = {}\n", + "\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " num_orders = int(input(\"How many different products would you like to order?\"))\n", + " if num_orders >= 0:\n", + " valid_number = True\n", + " else:\n", + " print(\"Number of orders can not be nagative. Please enter a valid number.\")\n", + " except ValueError:\n", + " print(\"Invalid Input. Please enter a numeric value. \")\n", + " \n", + " \n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product = input(\"Enter the product name: \")\n", + "\n", + " if product not in inventory:\n", + " print(\"Invalid product name. Please enter a product from the inventory.\")\n", + " elif inventory[product] == 0:\n", + " print(f\"Sorry, {product} is out of stock.\")\n", + " else:\n", + " valid_product = True\n", + " \n", + " if valid_product: \n", + " valid_quantity = False\n", + " while not valid_quantity:\n", + " try:\n", + " \n", + " quantity = int(input(f\"How many {product} would you like to order?\"))\n", + " if quantity < 0:\n", + " print(\"Quantity must be positive.\")\n", + " \n", + " elif quantity > inventory[product]:\n", + " print(\"Not enough stock available. Please enter a smaller number\")\n", + " else:\n", + " valid_quantity = True \n", + " orders[product] = quantity\n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric quantity.\")\n", + " \n", + " return orders " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84251d0e-b495-4726-8fa9-433dec89cd4c", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +180,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,