From 1770f640e7d7d3fc8f51acc50528867bc180c6b2 Mon Sep 17 00:00:00 2001 From: dianayule Date: Fri, 8 May 2026 15:51:07 +0200 Subject: [PATCH 1/3] Lab done --- lab-python-error-handling.ipynb | 195 +++++++++++++++++++++++++++++++- 1 file changed, 192 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..527ebcc 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,202 @@ "\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": null, + "id": "f10b9129-b538-4eb8-b85a-2d5911e4fb2d", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Define the function for initializing the inventory with error handling" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "66eb6ed7-2c82-40bf-b441-3b9f5b9300ec", + "metadata": {}, + "outputs": [], + "source": [ + "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": 2, + "id": "88972e6d-9649-4fe3-8bf2-c730aee9ad2e", + "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\n", + "#value), display an error message and ask them to re-enter the price for that \n", + "#product.\n", + "#Use a try-except block to handle the error and continue prompting the user \n", + "#until a valid price is entered." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "89f929cc-8735-4777-b590-c1804016a73a", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " order_items = customer_orders\n", + " price_list=[]\n", + " for item in order_items:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = int(input(f\"Enter the price of {item}s: \"))\n", + " if price >= 0:\n", + " price_list.append(price)\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + " total_price= sum(price_list) \n", + " return f\"The total price of your oder is: {total_price}\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00144927-04c2-4967-88df-5b01038c68e3", + "metadata": {}, + "outputs": [], + "source": [ + "#3. Modify the get_customer_orders function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "eb8918a0-28bf-455b-a7ff-9e4eef0423bd", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + " inventory_order= inventory\n", + " valid_order= False\n", + "\n", + " while not valid_order:\n", + " try:\n", + " norder=int(input(\"How many items do you want to order?\"))\n", + " valid_n=False\n", + " if norder<0:\n", + " print(\"Only positive numbers or zero are allowed.\")\n", + " elif norder==0:\n", + " valid_order=True\n", + " n=0\n", + " while norder>0 and n Date: Fri, 8 May 2026 17:30:55 +0200 Subject: [PATCH 2/3] Lab corrected --- lab-python-error-handling.ipynb | 187 ++++++++++++++++++++++---------- 1 file changed, 132 insertions(+), 55 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 527ebcc..4ecd7bf 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "f10b9129-b538-4eb8-b85a-2d5911e4fb2d", "metadata": {}, "outputs": [], @@ -85,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "id": "66eb6ed7-2c82-40bf-b441-3b9f5b9300ec", "metadata": {}, "outputs": [], @@ -109,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "id": "88972e6d-9649-4fe3-8bf2-c730aee9ad2e", "metadata": {}, "outputs": [], @@ -124,19 +124,18 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 5, "id": "89f929cc-8735-4777-b590-c1804016a73a", "metadata": {}, "outputs": [], "source": [ "def calculate_total_price(customer_orders):\n", - " order_items = customer_orders\n", " price_list=[]\n", - " for item in order_items:\n", + " for item in customer_orders:\n", " valid_input = False\n", " while not valid_input:\n", " try:\n", - " price = int(input(f\"Enter the price of {item}s: \"))\n", + " price = float(input(f\"Enter the price of {item}s: \"))\n", " if price >= 0:\n", " price_list.append(price)\n", " valid_input = True\n", @@ -145,12 +144,12 @@ " except ValueError:\n", " print(\"Invalid input. Please enter a valid price.\")\n", " total_price= sum(price_list) \n", - " return f\"The total price of your oder is: {total_price}\"" + " return f\"The total price of your order is: {total_price:.2f}\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "00144927-04c2-4967-88df-5b01038c68e3", "metadata": {}, "outputs": [], @@ -160,71 +159,134 @@ }, { "cell_type": "code", - "execution_count": 45, - "id": "eb8918a0-28bf-455b-a7ff-9e4eef0423bd", + "execution_count": 11, + "id": "81ac72ff-3b5a-4777-8560-ee8021df9a6b", "metadata": {}, "outputs": [], "source": [ "def get_customer_orders(inventory):\n", " customer_orders = set()\n", - " inventory_order= inventory\n", - " valid_order= False\n", - "\n", + " valid_order=False\n", " while not valid_order:\n", - " try:\n", - " norder=int(input(\"How many items do you want to order?\"))\n", - " valid_n=False\n", - " if norder<0:\n", - " print(\"Only positive numbers or zero are allowed.\")\n", - " elif norder==0:\n", + " try:\n", + " norder=int(input(\"How many items do you want to order?\"))\n", + " valid_quant=False\n", + " if norder==0:\n", + " valid_order=True\n", + " elif norder>0:\n", + " valid_quant= False\n", + " valid_order=True\n", + " else:\n", + " print(\"Enter a positive number or zero\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please number.\")\n", + " n=0 \n", + " while not valid_quant:\n", + " try:\n", + " item=input(\"Enter the name of the item:\")\n", + " \n", + " if item in inventory.keys() and inventory[item]>0:\n", + " customer_orders.add(item)\n", + " n+=1\n", + " if n==norder:\n", + " valid_quant=True\n", " valid_order=True\n", - " n=0\n", - " while norder>0 and n Date: Fri, 8 May 2026 17:39:12 +0200 Subject: [PATCH 3/3] Lab corrected2 --- lab-python-error-handling.ipynb | 102 +++++++++++++++----------------- 1 file changed, 47 insertions(+), 55 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 4ecd7bf..f7ac502 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,21 +75,13 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "f10b9129-b538-4eb8-b85a-2d5911e4fb2d", - "metadata": {}, - "outputs": [], - "source": [ - "# Step 1: Define the function for initializing the inventory with error handling" - ] - }, - { - "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "66eb6ed7-2c82-40bf-b441-3b9f5b9300ec", "metadata": {}, "outputs": [], "source": [ + "# Step 1: Define the function for initializing the inventory with error handling\n", + "\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -109,8 +101,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "id": "88972e6d-9649-4fe3-8bf2-c730aee9ad2e", + "execution_count": 2, + "id": "89f929cc-8735-4777-b590-c1804016a73a", "metadata": {}, "outputs": [], "source": [ @@ -119,16 +111,8 @@ "#value), display an error message and ask them to re-enter the price for that \n", "#product.\n", "#Use a try-except block to handle the error and continue prompting the user \n", - "#until a valid price is entered." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "89f929cc-8735-4777-b590-c1804016a73a", - "metadata": {}, - "outputs": [], - "source": [ + "#until a valid price is entered.\n", + "\n", "def calculate_total_price(customer_orders):\n", " price_list=[]\n", " for item in customer_orders:\n", @@ -149,21 +133,13 @@ }, { "cell_type": "code", - "execution_count": 6, - "id": "00144927-04c2-4967-88df-5b01038c68e3", - "metadata": {}, - "outputs": [], - "source": [ - "#3. Modify the get_customer_orders function to include error handling." - ] - }, - { - "cell_type": "code", - "execution_count": 11, + "execution_count": 3, "id": "81ac72ff-3b5a-4777-8560-ee8021df9a6b", "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_order=False\n", @@ -210,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 4, "id": "c0f04dfc-5215-4318-9d65-d9c98efbe027", "metadata": {}, "outputs": [ @@ -218,68 +194,68 @@ "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of cups available: f\n" + "Enter the quantity of cups available: -1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a valid quantity.\n" + "Quantity cannot be negative. Please enter a valid quantity.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of cups available: -1\n" + "Enter the quantity of cups available: a\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Quantity cannot be negative. Please enter a valid quantity.\n" + "Invalid input. Please enter a valid quantity.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "Enter the quantity of cups available: 2\n", - "Enter the quantity of mugs available: 1\n", - "Enter the quantity of hats available: 0\n", - "Enter the quantity of shoess available: 2\n", - "How many items do you want to order? sd\n" + "Enter the quantity of cups available: d\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please number.\n" + "Invalid input. Please enter a valid quantity.\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "How many items do you want to order? -2\n" + "Enter the quantity of cups available: 3\n", + "Enter the quantity of mugs available: 0\n", + "Enter the quantity of hats available: 1\n", + "Enter the quantity of shoess available: 2\n", + "How many items do you want to order? 2\n", + "Enter the name of the item: sd\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Enter a positive number or zero\n" + "The item does not exist in the inventory. We have ['cup', 'mug', 'hat', 'shoes']\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "How many items do you want to order? 2\n", - "Enter the name of the item: cupss\n" + "Enter the name of the item: -1\n" ] }, { @@ -294,7 +270,7 @@ "output_type": "stream", "text": [ "Enter the name of the item: cup\n", - "Enter the name of the item: hat\n" + "Enter the name of the item: mug\n" ] }, { @@ -308,18 +284,18 @@ "name": "stdin", "output_type": "stream", "text": [ - "Enter the name of the item: mug\n", - "Enter the price of mugs: 23\n", - "Enter the price of cups: 12.9\n" + "Enter the name of the item: hat\n", + "Enter the price of hats: 32\n", + "Enter the price of cups: 3.3\n" ] }, { "data": { "text/plain": [ - "'The total price of your order is: 35.90'" + "'The total price of your order is: 35.30'" ] }, - "execution_count": 12, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -338,6 +314,22 @@ "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2cb260b7-b77e-40a8-ba4b-7751939fa411", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a33bd362-3ce7-41b2-8834-28d0a4b248e1", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {