From 8ebbe8ad11d83936d4994b1df6a4272938be96de Mon Sep 17 00:00:00 2001 From: Alessia Mattera Date: Sat, 24 Jan 2026 16:33:15 +0100 Subject: [PATCH] Week 3 Lab 2 --- lab-python-error-handling.ipynb | 130 +++++++++++++++++++++++++++++++- 1 file changed, 128 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..7ffa5c6 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,137 @@ "\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": "markdown", + "id": "e5e40ddb", + "metadata": { + "vscode": { + "languageId": "plaintext" + } + }, + "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), 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." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "1423664e", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (1844852863.py, line 12)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[18], line 12\u001b[0;36m\u001b[0m\n\u001b[0;31m except ValueError:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "def calculate_total_price(customer_orders):\n", + " \n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = int(input(\"Enter price of the product: \"))\n", + " if price >= 0:\n", + " customer_orders[product] = price\n", + " break\n", + " else:\n", + " print(\"Price must be non-negative value:\")\n", + " except ValueError:\n", + " print(\"Enter a valid number:\")\n", + "\n", + "prices = { product: float(input(\"Enter the price of product: \")) for product in customer_orders}\n", + " total_price = sum(prices.values())\n", + " return total_price" + ] + }, + { + "cell_type": "markdown", + "id": "dba58348", + "metadata": {}, + "source": [ + "3. Modify the `get_customer_orders` function to include error handling.\n", + " - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n", + " - 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." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "dcf38fec", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + " valid_num_orders = False\n", + " \n", + " while not valid_num_orders:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders > 0:\n", + " valid_num_orders = True\n", + " else:\n", + " print(\"Number of orders must be a positive integer.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Number of orders must be a positive integer.\")\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", + " if product in inventory and inventory[product] > 0:\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " else:\n", + " print(\"Invalid product name or out of stock. Please enter a valid product.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid product.\")\n", + " \n", + " return customer_orders\n" + ] + }, + { + "cell_type": "markdown", + "id": "2c284275", + "metadata": {}, + "source": [ + "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": null, + "id": "a43fe9ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Number of orders must be a positive integer.\n", + "Invalid input. Number of orders must be a positive integer.\n" + ] + } + ], + "source": [ + "inventory = {\"apple\": 5, \"banana\": 3, \"orange\": 0}\n", + "customer_orders = get_customer_orders(inventory)\n", + "print(\"Customer orders:\", customer_orders)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +216,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,