From 5fadd6aa1442cc587e881f905bf26db001845cd6 Mon Sep 17 00:00:00 2001 From: Paul Omogiate Obamwonyi Date: Fri, 28 Aug 2026 16:25:42 +0200 Subject: [PATCH] completed lab-python-error-handling --- lab-python-error-handling.ipynb | 582 +++++++++++++++++++++++++++++++- 1 file changed, 579 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..011c94f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,589 @@ "\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": "7a394b8c-4506-4579-aa37-2cad1bf79316", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4159576f-d2cb-41b7-9b63-104453d53250", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(\n", + " input(f\"Enter the quantity of {product}s available: \")\n", + " )\n", + "\n", + " if quantity < 0:\n", + " raise ValueError(\n", + " \"Quantity cannot be negative.\"\n", + " )\n", + "\n", + " inventory[product] = quantity\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "236b8608-6260-4430-b66b-12340e16bda8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Quantity cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 2\n", + "Enter the quantity of mugs available: four\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'four'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 3\n", + "Enter the quantity of keychains available: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Final inventory:\n", + "{'t-shirt': 2, 'mug': 4, 'hat': 3, 'book': 3, 'keychain': 4}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(\"\\nFinal inventory:\")\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9d7292b6-c397-476b-9b31-87ec854cdf12", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(\n", + " input(f\"Enter the price of {product}: \")\n", + " )\n", + "\n", + " if price < 0:\n", + " raise ValueError(\n", + " \"Price cannot be negative.\"\n", + " )\n", + "\n", + " total_price += price\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "88d0d6d0-ee4c-4272-9b9b-396f6875bf5f", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: -6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Price cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of t-shirt: 6\n", + "Enter the price of mug: three\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: could not convert string to float: 'three'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total price: €9.00\n" + ] + } + ], + "source": [ + "test_orders = {\"t-shirt\", \"mug\"}\n", + "\n", + "total_price = calculate_total_price(test_orders)\n", + "\n", + "print(f\"\\nTotal price: €{total_price:.2f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3f307eb3-b1eb-4bef-8307-413295643457", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = []\n", + "\n", + " # Ask for a valid number of orders\n", + " while True:\n", + " try:\n", + " number_of_orders = int(\n", + " input(\"Enter the number of customer orders: \")\n", + " )\n", + "\n", + " if number_of_orders < 0:\n", + " raise ValueError(\n", + " \"The number of orders cannot be negative.\"\n", + " )\n", + "\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " # Ask for each product\n", + " for order_number in range(1, number_of_orders + 1):\n", + " while True:\n", + " try:\n", + " product = input(\n", + " f\"Enter product {order_number}: \"\n", + " ).strip().lower()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\n", + " f\"'{product}' is not available in the inventory.\"\n", + " )\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\n", + " f\"'{product}' is out of stock.\"\n", + " )\n", + "\n", + " customer_orders.append(product)\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "565a4301-adfb-4927-801d-4b7634e88de2", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: three\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'three'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 3\n", + "Enter product 1: t-shrts\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 't-shrts' is not available in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: book\n", + "Enter product 2: keychain\n", + "Enter product 3: hats\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'hats' is not available in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 3: hat\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'hat' is out of stock.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 3: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer orders:\n", + "['book', 'keychain', 'mug']\n" + ] + } + ], + "source": [ + "test_inventory = {\n", + " \"t-shirt\": 10,\n", + " \"mug\": 5,\n", + " \"hat\": 0,\n", + " \"book\": 8,\n", + " \"keychain\": 3\n", + "}\n", + "\n", + "customer_orders = get_customer_orders(test_inventory)\n", + "\n", + "print(\"\\nCustomer orders:\")\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "63272bdf-4798-48c5-9dea-b6b2a9e939a3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: five\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: 'five'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: -2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Quantity cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of mugs available: 3\n", + "Enter the quantity of hats available: 4\n", + "Enter the quantity of books available: 1\n", + "Enter the quantity of keychains available: 2.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: invalid literal for int() with base 10: '2.5'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of keychains available: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Inventory created:\n", + "{'t-shirt': 5, 'mug': 3, 'hat': 4, 'book': 1, 'keychain': 2}\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 4\n", + "Enter product 1: shoes\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'shoes' is not available in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 1: hat\n", + "Enter product 2: mug\n", + "Enter product 3: keychain\n", + "Enter product 4: t-shirts\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 't-shirts' is not available in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 4: hats\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'hats' is not available in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 4: books\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: 'books' is not available in the inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter product 4: book\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Customer orders:\n", + "['hat', 'mug', 'keychain', 'book']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 5\n", + "Enter the price of mug: five\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: could not convert string to float: 'five'\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of mug: 5\n", + "Enter the price of keychain: -3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Price cannot be negative.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain: 5\n", + "Enter the price of book: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total price: €19.00\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Step 1: Create the inventory\n", + "inventory = initialize_inventory(products)\n", + "\n", + "print(\"\\nInventory created:\")\n", + "print(inventory)\n", + "\n", + "# Step 2: Collect customer orders\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "print(\"\\nCustomer orders:\")\n", + "print(customer_orders)\n", + "\n", + "# Step 3: Calculate the total price\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(f\"\\nTotal price: €{total_price:.2f}\")" + ] + }, + { + "cell_type": "markdown", + "id": "6627d435-3e74-4fba-99ef-ff5b1dd5d18d", + "metadata": {}, + "source": [ + "### Error Handling Test Results\n", + "\n", + "The program successfully handles invalid user inputs without terminating unexpectedly. \n", + "`try-except` blocks catch non-numeric inputs, while manually raised \"value error\" exceptions handle negative values, unavailable products, and products without stock.\n", + "\n", + "The `while` loops repeatedly prompt the user until valid information is entered. After receiving valid inputs, the program creates the inventory, records the customer orders, and calculates the total price successfully." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc27b372-f89b-4e32-9f9a-0f88f24ed576", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:anaconda3] *", "language": "python", - "name": "python3" + "name": "conda-env-anaconda3-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +666,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,