From a8833cc81cf3a5c70e2749c877e16155ab4f8d73 Mon Sep 17 00:00:00 2001 From: carmenlnr Date: Thu, 16 Apr 2026 17:49:42 +0200 Subject: [PATCH 1/5] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 115 +++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..b062130 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -70,13 +70,122 @@ " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. *Hint: you will need to pass inventory as a parameter*\n", " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n", "\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" + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1e8a5818", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid price.\n", + "Price cannot be negative. Please enter a valid price.\n", + "Invalid input. Please enter a valid price.\n", + "Price cannot be negative. Please enter a valid price.\n", + "Total: 32.0\n" + ] + } + ], + "source": [ + "# 2 \n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " else:\n", + " total_price += price\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + "\n", + " return total_price\n", + "customer_orders= {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n", + "total = calculate_total_price(customer_orders)\n", + "print(\"Total:\", total)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "99d89665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Number of orders cannot be negative.\n", + "Error: invalid literal for int() with base 10: 'abc'\n", + "Error: Product not found in inventory.\n", + "Error: Product is out of stock.\n", + "Error: Product not found in inventory.\n", + "{'mug', 'hat'}\n" + ] + } + ], + "source": [ + "# 3\n", + "inventory = {\n", + " \"t-shirt\": 10,\n", + " \"mug\": 5,\n", + " \"hat\": 1,\n", + " \"book\": 3,\n", + " \"keychain\": 0\n", + "}\n", + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " valid_input = True\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\"Product not found in inventory.\")\n", + " elif inventory[product] == 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + "\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + "\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " return customer_orders\n", + "\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(customer_orders)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +199,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.3" } }, "nbformat": 4, From 638e6937f5690fd3783432218348ca2137fcfa08 Mon Sep 17 00:00:00 2001 From: carmenlnr Date: Fri, 17 Apr 2026 20:17:16 +0200 Subject: [PATCH 2/5] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 98 ++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index b062130..f8e3d4f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,44 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, + "id": "e8160c53", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Quantity cannot be negative.. please enter a valid quantity\n", + "Error: invalid literal for int() with base 10: 'j'. please enter a valid quantity\n" + ] + } + ], + "source": [ + "# 1 \n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity for {product}: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}. please enter a valid quantity\")\n", + "\n", + " return inventory\n", + "inventory = initialize_inventory (products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, "id": "1e8a5818", "metadata": {}, "outputs": [ @@ -83,11 +120,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a valid price.\n", - "Price cannot be negative. Please enter a valid price.\n", - "Invalid input. Please enter a valid price.\n", - "Price cannot be negative. Please enter a valid price.\n", - "Total: 32.0\n" + "Error: Price cannot be negative. Please enter a valid price\n", + "Error: could not convert string to float: 'a'. Please enter a valid price\n" ] } ], @@ -98,29 +132,30 @@ " total_price = 0\n", "\n", " for product in customer_orders:\n", - " valid_input = False\n", - " while not valid_input:\n", + " while True:\n", " try:\n", " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", " if price < 0:\n", - " print(\"Price cannot be negative. Please enter a valid price.\")\n", - " else:\n", - " total_price += price\n", - " valid_input = True\n", - " except ValueError:\n", - " print(\"Invalid input. Please enter a valid price.\")\n", + " raise ValueError(\"Price cannot be negative\")\n", + " \n", + " total_price += price\n", + " break\n", + "\n", + " except ValueError as e:\n", + " print(f\"Error: {e}. Please enter a valid price\")\n", "\n", " return total_price\n", - "customer_orders= {\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"}\n", + "customer_orders= [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "total = calculate_total_price(customer_orders)\n", - "print(\"Total:\", total)\n", + "\n", "\n", "\n" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "id": "99d89665", "metadata": {}, "outputs": [ @@ -129,11 +164,10 @@ "output_type": "stream", "text": [ "Error: Number of orders cannot be negative.\n", - "Error: invalid literal for int() with base 10: 'abc'\n", - "Error: Product not found in inventory.\n", - "Error: Product is out of stock.\n", + "Error: invalid literal for int() with base 10: 'b'\n", "Error: Product not found in inventory.\n", - "{'mug', 'hat'}\n" + "Error: Product out of stock.\n", + "['hat', 'mug']\n" ] } ], @@ -147,31 +181,29 @@ " \"keychain\": 0\n", "}\n", "def get_customer_orders(inventory):\n", - " valid_input = False\n", - " while not valid_input:\n", + " while True:\n", " try:\n", " num_orders = int(input(\"Enter the number of customer orders: \"))\n", " if num_orders < 0:\n", " raise ValueError(\"Number of orders cannot be negative.\")\n", - " valid_input = True\n", + " break\n", " except ValueError as e:\n", " print(f\"Error: {e}\")\n", "\n", - " customer_orders = set()\n", + " customer_orders = []\n", "\n", " for _ in range(num_orders):\n", - " valid_product = False\n", - " while not valid_product:\n", - " try:\n", - " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + " while True:\n", + " product = input(\"Enter product name: \").strip().lower()\n", "\n", + " try:\n", " if product not in inventory:\n", " raise ValueError(\"Product not found in inventory.\")\n", - " elif inventory[product] == 0:\n", - " raise ValueError(\"Product is out of stock.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(\"Product out of stock.\")\n", "\n", - " customer_orders.add(product)\n", - " valid_product = True\n", + " customer_orders.append(product)\n", + " break\n", "\n", " except ValueError as e:\n", " print(f\"Error: {e}\")\n", From cf8fc05444be394ef407260e0125ced0f1491eb0 Mon Sep 17 00:00:00 2001 From: carmenlnr Date: Fri, 17 Apr 2026 20:28:34 +0200 Subject: [PATCH 3/5] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 60 +++++++-------------------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f8e3d4f..8f92014 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 13, "id": "e8160c53", "metadata": {}, "outputs": [ @@ -84,7 +84,14 @@ "output_type": "stream", "text": [ "Error: Quantity cannot be negative.. please enter a valid quantity\n", - "Error: invalid literal for int() with base 10: 'j'. please enter a valid quantity\n" + "Error: invalid literal for int() with base 10: 'l'. please enter a valid quantity\n", + "Error: Quantity cannot be negative.. please enter a valid quantity\n", + "Error: invalid literal for int() with base 10: 'f'. please enter a valid quantity\n", + "Error: Price cannot be negative. Please enter a valid price\n", + "Error: could not convert string to float: 'd'. Please enter a valid price\n", + "Error: Product not found in inventory.\n", + "Error: Product out of stock.\n", + "['mug', 'hat']\n" ] } ], @@ -107,27 +114,9 @@ " print(f\"Error: {e}. please enter a valid quantity\")\n", "\n", " return inventory\n", - "inventory = initialize_inventory (products)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "1e8a5818", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Error: Price cannot be negative. Please enter a valid price\n", - "Error: could not convert string to float: 'a'. Please enter a valid price\n" - ] - } - ], - "source": [ - "# 2 \n", + "inventory = initialize_inventory (products)\n", "\n", + "#2\n", "def calculate_total_price(customer_orders):\n", " total_price = 0\n", "\n", @@ -149,30 +138,7 @@ "customer_orders= [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "total = calculate_total_price(customer_orders)\n", "\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "99d89665", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Error: Number of orders cannot be negative.\n", - "Error: invalid literal for int() with base 10: 'b'\n", - "Error: Product not found in inventory.\n", - "Error: Product out of stock.\n", - "['hat', 'mug']\n" - ] - } - ], - "source": [ - "# 3\n", + "#3\n", "inventory = {\n", " \"t-shirt\": 10,\n", " \"mug\": 5,\n", @@ -211,7 +177,7 @@ " return customer_orders\n", "\n", "customer_orders = get_customer_orders(inventory)\n", - "print(customer_orders)" + "print(customer_orders)\n" ] } ], From 95f443cb253f26ba52958cd1d69efa8007dcc0c5 Mon Sep 17 00:00:00 2001 From: carmenlnr Date: Sun, 19 Apr 2026 23:23:14 +0200 Subject: [PATCH 4/5] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 46 ++++++++++++--------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 8f92014..364bfdc 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 16, "id": "e8160c53", "metadata": {}, "outputs": [ @@ -83,15 +83,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "Error: Quantity cannot be negative.. please enter a valid quantity\n", - "Error: invalid literal for int() with base 10: 'l'. please enter a valid quantity\n", - "Error: Quantity cannot be negative.. please enter a valid quantity\n", - "Error: invalid literal for int() with base 10: 'f'. please enter a valid quantity\n", - "Error: Price cannot be negative. Please enter a valid price\n", - "Error: could not convert string to float: 'd'. Please enter a valid price\n", - "Error: Product not found in inventory.\n", + "Error: Quantity cannot be negative.. Please enter a valid quantity\n", + "Error: invalid literal for int() with base 10: 'e'. Please enter a valid quantity\n", + "Invalid input. Please re-enter the price.\n", + "Invalid input. Please re-enter the price.\n", + "Error: Number of orders cannot be negative.. Please enter a valid number\n", + "Error: invalid literal for int() with base 10: 'f'. Please enter a valid number\n", + "Error: 'chair' not found in inventory.\n", + "Error: 'bool' not found in inventory.\n", "Error: Product out of stock.\n", - "['mug', 'hat']\n" + "['hat', 'keychain']\n" ] } ], @@ -111,7 +112,7 @@ " inventory[product] = quantity\n", " break\n", " except ValueError as e:\n", - " print(f\"Error: {e}. please enter a valid quantity\")\n", + " print(f\"Error: {e}. Please enter a valid quantity\")\n", "\n", " return inventory\n", "inventory = initialize_inventory (products)\n", @@ -124,47 +125,34 @@ " while True:\n", " try:\n", " price = float(input(f\"Enter the price of {product}: \"))\n", - "\n", " if price < 0:\n", " raise ValueError(\"Price cannot be negative\")\n", - " \n", " total_price += price\n", " break\n", - "\n", " except ValueError as e:\n", - " print(f\"Error: {e}. Please enter a valid price\")\n", + " print(\"Invalid input. Please re-enter the price.\")\n", "\n", " return total_price\n", - "customer_orders= [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "total = calculate_total_price(customer_orders)\n", + "total_price= calculate_total_price(customer_orders)\n", "\n", "#3\n", - "inventory = {\n", - " \"t-shirt\": 10,\n", - " \"mug\": 5,\n", - " \"hat\": 1,\n", - " \"book\": 3,\n", - " \"keychain\": 0\n", - "}\n", "def get_customer_orders(inventory):\n", " while True:\n", " try:\n", " num_orders = int(input(\"Enter the number of customer orders: \"))\n", - " if num_orders < 0:\n", + " if num_orders <= 0:\n", " raise ValueError(\"Number of orders cannot be negative.\")\n", " break\n", " except ValueError as e:\n", - " print(f\"Error: {e}\")\n", + " print(f\"Error: {e}. Please enter a valid number\")\n", "\n", " customer_orders = []\n", - "\n", " for _ in range(num_orders):\n", " while True:\n", - " product = input(\"Enter product name: \").strip().lower()\n", - "\n", " try:\n", + " product = input(\"Enter product name: \").strip().lower()\n", " if product not in inventory:\n", - " raise ValueError(\"Product not found in inventory.\")\n", + " raise ValueError(f\"'{product}' not found in inventory.\")\n", " if inventory[product] == 0:\n", " raise ValueError(\"Product out of stock.\")\n", "\n", From 25afe912f4822161bb3432ff70b0232f7b8047ad Mon Sep 17 00:00:00 2001 From: carmenlnr Date: Sun, 19 Apr 2026 23:49:06 +0200 Subject: [PATCH 5/5] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 364bfdc..4697dd3 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "e8160c53", "metadata": {}, "outputs": [ @@ -115,7 +115,6 @@ " print(f\"Error: {e}. Please enter a valid quantity\")\n", "\n", " return inventory\n", - "inventory = initialize_inventory (products)\n", "\n", "#2\n", "def calculate_total_price(customer_orders):\n", @@ -130,17 +129,16 @@ " total_price += price\n", " break\n", " except ValueError as e:\n", - " print(\"Invalid input. Please re-enter the price.\")\n", + " print(f\"Error: {e}. Please re-enter the price.\")\n", "\n", " return total_price\n", - "total_price= calculate_total_price(customer_orders)\n", "\n", "#3\n", "def get_customer_orders(inventory):\n", " while True:\n", " try:\n", " num_orders = int(input(\"Enter the number of customer orders: \"))\n", - " if num_orders <= 0:\n", + " if num_orders < 0:\n", " raise ValueError(\"Number of orders cannot be negative.\")\n", " break\n", " except ValueError as e:\n", @@ -164,7 +162,9 @@ "\n", " return customer_orders\n", "\n", + "inventory = initialize_inventory(products)\n", "customer_orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price (customer_orders)\n", "print(customer_orders)\n" ] }