From 4c391642cef814f99a76460e5fbba05e72f0859f Mon Sep 17 00:00:00 2001 From: Hanane Mamalik Date: Fri, 20 Feb 2026 23:46:00 +0100 Subject: [PATCH 1/2] lab solved error handling --- lab-python-error-handling.ipynb | 76 ++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..f1f1d30 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,83 @@ "\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": "73e7b722", + "metadata": {}, + "outputs": [], + "source": [ + "#q2 modify 'calculate_total_price' function to include error handling\n", + "\n", + "def calculate_total_price (customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " price = int(input(f\"please enter price of {product}:\"))\n", + " while price < 0:\n", + " price=float(input(f\"Price cannot be negative, Please enter the price for {product}:\"))\n", + " total_price += price\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "dcecd7e9", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price (customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price :\n", + " try:\n", + " price = int(input(f\"please enter price of {product}:\"))\n", + " if price < 0 :\n", + " print(\"error: Price cannot be negative\")\n", + " else:\n", + " total_price += price\n", + " valid_price = True\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number.\")\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5ddf33d3", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders ():\n", + " customer_orders =set()\n", + " answer =\"yes\"\n", + " \n", + " while answer == \"yes\":\n", + " try:\n", + " product = input(\"Enter name of product: \")\n", + " if product\n", + " \n", + " customer_orders.add(product)\n", + " answer = input(\"Do you want to add another product? (yes/no): \")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9febb905", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +162,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From a4e7dd3acb7b87f3454660ade8d3568fff6fa8b2 Mon Sep 17 00:00:00 2001 From: Hanane Mamalik Date: Sat, 21 Feb 2026 00:08:16 +0100 Subject: [PATCH 2/2] lab error handling ver 2 --- lab-python-error-handling.ipynb | 133 +++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 36 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f1f1d30..be7fa13 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,75 +75,136 @@ }, { "cell_type": "code", - "execution_count": 1, - "id": "73e7b722", + "execution_count": 10, + "id": "3936290e", "metadata": {}, "outputs": [], "source": [ - "#q2 modify 'calculate_total_price' function to include 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", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c745f8ac", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = []\n", + " \n", + " while True:\n", + " product = input(\"Enter product name (or type 'done' to finish): \")\n", "\n", - "def calculate_total_price (customer_orders):\n", - " total_price = 0\n", - " for product in customer_orders:\n", - " price = int(input(f\"please enter price of {product}:\"))\n", - " while price < 0:\n", - " price=float(input(f\"Price cannot be negative, Please enter the price for {product}:\"))\n", - " total_price += price\n", - " return total_price" + " if product.lower() == \"done\":\n", + " break\n", + "\n", + " if product not in inventory:\n", + " print(\"Product does not exist. Try again.\")\n", + " elif inventory[product] == 0:\n", + " print(\"Product is out of stock.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " inventory[product] -= 1\n", + " print(product + \" added to the order.\")\n", + "\n", + " return customer_orders\n" ] }, { "cell_type": "code", - "execution_count": 2, - "id": "dcecd7e9", + "execution_count": 8, + "id": "956c322d", "metadata": {}, "outputs": [], "source": [ - "def calculate_total_price (customer_orders):\n", + "def calculate_total_price(customer_orders):\n", " total_price = 0\n", + "\n", " for product in customer_orders:\n", " valid_price = False\n", - " while not valid_price :\n", + "\n", + " while not valid_price:\n", " try:\n", - " price = int(input(f\"please enter price of {product}:\"))\n", - " if price < 0 :\n", - " print(\"error: Price cannot be negative\")\n", + " price = int(input(f\"Enter price for {product}: \"))\n", + "\n", + " if price < 0:\n", + " print(\"Price cannot be negative.\")\n", " else:\n", " total_price += price\n", " valid_price = True\n", + "\n", " except ValueError:\n", - " print(\"Error: Please enter a valid number.\")\n", + " print(\"Please enter a valid number.\")\n", + "\n", " return total_price" ] }, { "cell_type": "code", - "execution_count": null, - "id": "5ddf33d3", + "execution_count": 11, + "id": "52839e7c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Product does not exist. try again\n", + "Product added.\n", + "Customer ordered: {'apple'}\n", + "Total price: 2\n", + "Updated inventory: {'apple': 2, 'banana': 4, 'orange': 1, 'grapes': 2}\n" + ] + } + ], "source": [ - "def get_customer_orders ():\n", - " customer_orders =set()\n", - " answer =\"yes\"\n", - " \n", - " while answer == \"yes\":\n", - " try:\n", - " product = input(\"Enter name of product: \")\n", - " if product\n", - " \n", - " customer_orders.add(product)\n", - " answer = input(\"Do you want to add another product? (yes/no): \")\n", - " return customer_orders" + "products = [\"apple\", \"banana\", \"orange\", \"grapes\"]\n", + "\n", + "# Initialize inventory\n", + "inventory = initialize_inventory(products)\n", + "\n", + "# Get orders\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "# Calculate total price\n", + "total = calculate_total_price(customer_orders)\n", + "\n", + "# Print results\n", + "print(\"Customer ordered:\", customer_orders)\n", + "print(\"Total price:\", total)\n", + "print(\"Updated inventory:\", inventory)" ] }, { "cell_type": "code", "execution_count": null, - "id": "9febb905", + "id": "86d2753a", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "\n" + ] } ], "metadata": {