diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..276672e 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,211 @@ "\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": 6, + "id": "464b1353", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1:\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", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b4154ae6", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2:\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0.0\n", + " \n", + " for product in customer_orders:\n", + " while True:\n", + " entry = input(f\"Enter the price of {product}: \")\n", + " try:\n", + " price = float(entry)\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please try again.\")\n", + " else:\n", + " total_price += price\n", + " break\n", + " except ValueError:\n", + " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", + " \n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c568692b", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 3:\n", + "def get_customer_orders(inventory):\n", + " # 1. Validate the number of orders\n", + " while True:\n", + " try:\n", + " entry = input(\"How many products do you want to order? \")\n", + " num_orders = int(entry)\n", + " if num_orders < 0:\n", + " print(\"Error: The number of orders cannot be negative.\")\n", + " continue\n", + " break\n", + " except ValueError:\n", + " print(f\"Error: '{entry}' is not a valid number. Please enter an integer.\")\n", + " \n", + " # 2. Collect valid product names\n", + " customer_orders = set()\n", + " \n", + " for i in range(num_orders):\n", + " while True:\n", + " product_name = input(f\"Enter product {i + 1}: \")\n", + " \n", + " # Check if product exists in inventory\n", + " if product_name not in inventory:\n", + " print(f\"Error: '{product_name}' is not in our inventory.\")\n", + " print(f\"Available products: {', '.join(inventory.keys())}\")\n", + " continue\n", + " \n", + " # Check if product has stock available\n", + " if inventory[product_name] <= 0:\n", + " print(f\"Error: '{product_name}' is out of stock.\")\n", + " continue\n", + " \n", + " # Valid product with stock available\n", + " customer_orders.add(product_name)\n", + " break\n", + " \n", + " return customer_orders\n", + "\n", + "\n", + " \n", + " \n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5375f1f8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Summary of your order: {'hat', 'book', 'mug'}\n", + "\n", + "Final Inventory after orders:\n", + " t-shirt: 2\n", + " mug: 2\n", + " hat: 3\n", + " book: 4\n", + " keychain: 1\n" + ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def manage_customer_orders():\n", + " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + " inventory = initialize_inventory(products_list)\n", + " customer_orders = get_customer_orders(inventory)\n", + "\n", + " print(f\"\\nSummary of your order: {customer_orders}\") # ✅ fixed f-string\n", + "\n", + " for product in customer_orders: # ✅ iterate set directly, no .items()\n", + " inventory[product] -= 1 # ✅ subtract 1 per product\n", + "\n", + " print(\"\\nFinal Inventory after orders:\")\n", + " for product, stock in inventory.items():\n", + " print(f\" {product}: {stock}\")\n", + " return customer_orders\n", + "manage_customer_orders()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "621c5fc9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n", + "Error: 'sock' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: 'ten' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '10' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n" + ] + } + ], + "source": [ + "def manage_customer_orders():\n", + " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + " inventory = initialize_inventory(products_list)\n", + " customer_orders = get_customer_orders(inventory)\n", + "\n", + " print(f\"\\nSummary of your order: {customer_orders}\") # ✅ fixed f-string\n", + "\n", + " for product in customer_orders: # ✅ iterate set directly, no .items()\n", + " inventory[product] -= 1 # ✅ subtract 1 per product\n", + "\n", + " print(\"\\nFinal Inventory after orders:\")\n", + " for product, stock in inventory.items():\n", + " print(f\" {product}: {stock}\")\n", + " return customer_orders\n", + "manage_customer_orders()" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +290,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,