diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..1fbfb18 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,150 @@ "\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": null, + "id": "ebabdc77", + "metadata": {}, + "outputs": [], + "source": [ + "# Exercise: Error Handling for Managing Customer Orders\n", + "# Def function for initializing the inventory with error handling\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " valid_quantity = False\n", + "\n", + " while not valid_quantity:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + "\n", + " if quantity < 0:\n", + " print(\"Error: Quantity cannot be negative. Please enter a valid quantity.\")\n", + " else:\n", + " inventory[product] = quantity\n", + " valid_quantity = True\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a valid quantity.\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "4c48d1e0", + "metadata": {}, + "outputs": [], + "source": [ + "# Calculate total price with error handling\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_price = False\n", + "\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + "\n", + " if price < 0:\n", + " print(\"Error: Invalid price! Please enter a non-negative value.\")\n", + " else:\n", + " total_price += price\n", + " valid_price = True\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9686b539", + "metadata": {}, + "outputs": [], + "source": [ + "# Modify the get_customer_orders function to include error handling\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if number_of_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative.\")\n", + " else:\n", + " valid_number = True\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a whole number.\")\n", + "\n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " product = input(f\"Enter product {i+1}: \").strip().lower()\n", + "\n", + " if product not in inventory:\n", + " print(f\"Error: '{product}' is not in the inventory.\")\n", + " elif inventory[product] <= 0:\n", + " print(f\"Error: '{product}' is out of stock.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " inventory[product] -= 1\n", + " valid_product = True\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "85a56ef4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid input. Please enter a valid quantity.\n", + "Error: Quantity cannot be negative. Please enter a valid quantity.\n", + "Error: Invalid input. Please enter a valid quantity.\n", + "Error: 'bokk' is not in the inventory.\n", + "Error: Invalid price! Please enter a non-negative value.\n", + "Inventory: {'t-shirt': 66, 'mug': 64, 'hat': 87, 'book': 99, 'keychain': 77}\n", + "Customer Orders: {'hat', 'mug', 'book', 'keychain'}\n", + "Total Price: 102.65\n" + ] + } + ], + "source": [ + "# Test the program with invalid inputs\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(\"Inventory:\", inventory)\n", + "print(\"Customer Orders:\", customer_orders)\n", + "print(\"Total Price:\", total_price)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +229,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,