From 2487e567ed88be20542f3cebb2e27c663e26b581 Mon Sep 17 00:00:00 2001 From: durgainnovative Date: Wed, 18 Mar 2026 09:26:04 +0100 Subject: [PATCH] Error Handle_lab --- ...lab-python-error-handling-checkpoint.ipynb | 221 ++++++++++++++++++ anaconda_projects/db/project_filebrowser.db | Bin 0 -> 32768 bytes lab-python-error-handling.ipynb | 125 +++++++++- 3 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb create mode 100644 anaconda_projects/db/project_filebrowser.db diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb new file mode 100644 index 0000000..3daeb4d --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,221 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b", + "metadata": {}, + "source": [ + "## Exercise: Error Handling for Managing Customer Orders\n", + "\n", + "The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n", + "\n", + "For example, we could modify the `initialize_inventory` function to include error handling.\n", + " - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n", + " - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n", + "\n", + "```python\n", + "# 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}s 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", + "\n", + "# Or, in another way:\n", + "\n", + "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\n", + "```\n", + "\n", + "Let's enhance your code by implementing error handling to handle invalid inputs.\n", + "\n", + "Follow the steps below to complete the exercise:\n", + "\n", + "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.\n", + "\n", + "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.\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" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "947b315f-432e-464e-a25a-3fcbfce99b88", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2461498180.py, line 34)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[24]\u001b[39m\u001b[32m, line 34\u001b[39m\n\u001b[31m \u001b[39m\u001b[31m```\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "d28d7403-9d5f-48b0-8598-f11676d596fb", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 41\u001b[39m\n\u001b[32m 38\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInvalid input. Please enter a valid number.\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 40\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m orders\n\u001b[32m---> \u001b[39m\u001b[32m41\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInventory:\u001b[39m\u001b[33m\"\u001b[39m, \u001b[43minventory\u001b[49m)\n\u001b[32m 42\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mOrders:\u001b[39m\u001b[33m\"\u001b[39m, orders)\n\u001b[32m 43\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mTotal price:\u001b[39m\u001b[33m\"\u001b[39m, total_price)\n", + "\u001b[31mNameError\u001b[39m: name 'inventory' is not defined" + ] + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = {}\n", + "\n", + " # Validate number of orders\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of different products you want to order: \"))\n", + " if num_orders > 0:\n", + " break\n", + " else:\n", + " print(\"Number of orders must be greater than 0.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " # Validate each product entry\n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter the product name: \").lower()\n", + "\n", + " if product not in inventory:\n", + " print(\"Product does not exist. Please enter a valid product.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Sorry, this product is out of stock. Choose another product.\")\n", + " else:\n", + " break\n", + "\n", + " # Validate quantity\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity of {product}: \"))\n", + " if 0 < quantity <= inventory[product]:\n", + " orders[product] = quantity\n", + " inventory[product] -= quantity\n", + " break\n", + " else:\n", + " print(\"Invalid quantity. Must be positive and within available stock.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " return orders\n", + "print(initialize_inventory) \n", + "print(calculate_total_price)\n", + "print()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e2d8afde-6721-4556-a297-1c7b1af0ece6", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(orders):\n", + " total_price = 0\n", + "\n", + " for product, quantity in orders.items():\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", + " total_price += price * quantity\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 numeric value.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36352fa8-f89a-4181-9025-93c3e8b199dc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b3b3757-9d38-477a-a5e8-9ad3b0479895", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/anaconda_projects/db/project_filebrowser.db b/anaconda_projects/db/project_filebrowser.db new file mode 100644 index 0000000000000000000000000000000000000000..d7e75dbf5ff0562ac63cbf814352f2ce6bc50b21 GIT binary patch literal 32768 zcmeI)PjAv-9Ki9GZT#Ch^u&qe$&zJ|Et)Z>@nB-b8W)iXE5^l`rh^Algu=XvOp=lS%PCQn*8s+Jv3idJvXwLP(?+)`9k zc`SsYD64)X{fPS!^CRuYvLE55h6gw7SCub009ILKmY**5cod?#<$~%Y%ZsM9(ndjNA?H3wrqIAaKBut8Mz(-g1uTIso76O}_#RO;pOj!37Pwr2~|cw+{I-bYssu1c=mmD55~w(L>Iv!r|G z40>)?y53c9PmO1Ky=;oDt(lHJ`~7fouDEJ``)>9N>C~&*!Are%EM6GLBJVT{!2n8! zl_RsJA5=^+I<+Rlv4UY(p6vETy>d{k8zMic-w{DWAziqeh-XVVRdL*={4_l6_$xNc z9(lb<*$NG;-S8+;&0MN0f#SFtDxUYJu6tq4KkE7y*^+<6Rt(va=LI#9-QHHmsfm%@ zk=+xgVV%jr(CN7g#n@aXE;I(|I6cjBN8K=!@_WU?ObX3q!$J>M|3gE}CD2TNeoj1! z#k1Az-{r(&XS)j(Oa9FySlcU_A8O{O;(yo>KmY**5I_I{1Q0*~0R#|0AR+?mYHYLh zVE^I%$-Voa+fj$i-zE2q1s}0tg_000IagfB*srOa(Gao2l9LfAIX@f9anM z0R#|0009ILKmY**5I_I{1R^ZJ`ai-Km#HCu00IagfB*srAb|BuSmWF810fB*srAb \u001b[39m\u001b[32m41\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInventory:\u001b[39m\u001b[33m\"\u001b[39m, \u001b[43minventory\u001b[49m)\n\u001b[32m 42\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mOrders:\u001b[39m\u001b[33m\"\u001b[39m, orders)\n\u001b[32m 43\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mTotal price:\u001b[39m\u001b[33m\"\u001b[39m, total_price)\n", + "\u001b[31mNameError\u001b[39m: name 'inventory' is not defined" + ] + } + ], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = {}\n", + "\n", + " # Validate number of orders\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of different products you want to order: \"))\n", + " if num_orders > 0:\n", + " break\n", + " else:\n", + " print(\"Number of orders must be greater than 0.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " # Validate each product entry\n", + " for _ in range(num_orders):\n", + " while True:\n", + " product = input(\"Enter the product name: \").lower()\n", + "\n", + " if product not in inventory:\n", + " print(\"Product does not exist. Please enter a valid product.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Sorry, this product is out of stock. Choose another product.\")\n", + " else:\n", + " break\n", + "\n", + " # Validate quantity\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter quantity of {product}: \"))\n", + " if 0 < quantity <= inventory[product]:\n", + " orders[product] = quantity\n", + " inventory[product] -= quantity\n", + " break\n", + " else:\n", + " print(\"Invalid quantity. Must be positive and within available stock.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " return orders\n", + "print(initialize_inventory) \n", + "print(calculate_total_price)\n", + "print()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e2d8afde-6721-4556-a297-1c7b1af0ece6", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(orders):\n", + " total_price = 0\n", + "\n", + " for product, quantity in orders.items():\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", + " total_price += price * quantity\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 numeric value.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36352fa8-f89a-4181-9025-93c3e8b199dc", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b3b3757-9d38-477a-a5e8-9ad3b0479895", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +213,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,