diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..f7ac502 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,271 @@ "\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": "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", + " 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": "89f929cc-8735-4777-b590-c1804016a73a", + "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.\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " price_list=[]\n", + " for item in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\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", + " 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 order is: {total_price:.2f}\"" + ] + }, + { + "cell_type": "code", + "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", + " while not valid_order:\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", + " elif item in inventory.keys() and inventory[item]==0:\n", + " print(\"Sorry, we are out of stock for that item.\") \n", + " else:\n", + " print(f\"The item does not exist in the inventory. We have {list(inventory.keys())}\") \n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid item\") \n", + " return customer_orders\n", + " \n", + " \n", + " \n", + "\n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c0f04dfc-5215-4318-9d65-d9c98efbe027", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of cups available: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quantity cannot be negative. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of cups available: a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of cups available: d\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "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": [ + "The item does not exist in the inventory. We have ['cup', 'mug', 'hat', 'shoes']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the item: -1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The item does not exist in the inventory. We have ['cup', 'mug', 'hat', 'shoes']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of the item: cup\n", + "Enter the name of the item: mug\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sorry, we are out of stock for that item.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "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.30'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products=['cup','mug','hat','shoes']\n", + "inventory=initialize_inventory(products)\n", + "customer_orders=get_customer_orders(inventory)\n", + "calculate_total_price(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87807f1b-3f74-47c4-a5b2-ad960491bb5d", + "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": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +348,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,