From 31cf383f525e04338f5f1924d504fe02397dca38 Mon Sep 17 00:00:00 2001 From: zeineb Date: Mon, 24 Nov 2025 22:52:46 +0100 Subject: [PATCH] error handling --- lab-python-error-handling.ipynb | 210 +++++++++++++++++++++++++++++++- 1 file changed, 209 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..099b1a8 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,214 @@ "\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": 57, + "id": "436f531c-b4ca-4986-94da-272cd461a36c", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "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} 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\n" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "61dc3008-cc2f-4a19-9575-52b31870a560", + "metadata": {}, + "outputs": [], + "source": [ + "#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), \n", + "#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", + "def calculate_total_price():\n", + " price_list= []\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = int(input(f\"Enter the Enter the price of: {product}\"))\n", + " if price <0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " else:\n", + " valid_price = True\n", + " price_list.append(price) \n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return sum(price_list) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "fda579cd-67d6-493f-8095-c5bc453191c2", + "metadata": {}, + "outputs": [], + "source": [ + "#3 Modify the get_customer_orders function to include error handling.\n", + "\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + " valid_digit = False\n", + " while not valid_digit:\n", + " try:\n", + " order_number = int(input(\"Enter the number of customer orders: \"))\n", + " if order_number <0:\n", + " raise ValueError(\"Invalid order number! Please enter a non-negative value.\")\n", + " else:\n", + " valid_digit = True \n", + " except ValueError as error:\n", + " print(f\"Error: {error}\") \n", + "\n", + " for i in range(0, order_number):\n", + " product_not_available = True\n", + " while product_not_available:\n", + " try:\n", + " order = input(\"Enter the name of a product that a customer wants to order: \")\n", + " if order not in inventory:\n", + " raise ValueError(\"product not available, please choose another one.\")\n", + " elif inventory.get(order) == 0:\n", + " raise ValueError(\"product not in stock, please choose another one.\") \n", + " else:\n", + " customer_orders.add(order)\n", + " product_not_available = False\n", + " except (NameError,ValueError) as error:\n", + " print(f\"Error: {error}\")\n", + " \n", + " return customer_orders \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "ef76be67-3026-4a96-a7cc-aef61ffa8b68", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "add a list of products separated by , book,hat,mug\n", + "Enter the quantity of book available: 11\n", + "Enter the quantity of hat available: 0\n", + "Enter the quantity of mug available: 11\n", + "Enter the Enter the price of: book 11\n", + "Enter the Enter the price of: hat 11\n", + "Enter the Enter the price of: mug 11\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the price is 33\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: book\n", + "Enter the name of a product that a customer wants to order: kkkk\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product not available, please choose another one.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: aa\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product not available, please choose another one.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: product not in stock, please choose another one.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'mug'}" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#4 Test your code by running the program and deliberately entering invalid quantities and product names. \n", + "#Make sure the error handling mechanism works as expected.\n", + "\n", + "user_products = input(\"add a list of products separated by ,\")\n", + "try:\n", + " products = user_products.strip().split(\",\")\n", + "except:\n", + " print(\"error in creating products\")\n", + "\n", + "inventory= initialize_inventory(products)\n", + "price = calculate_total_price()\n", + "print(f\"the price is {price}\")\n", + "get_customer_orders(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c267f47b-79cd-4ae7-8784-150de9b3c13a", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,