diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..b2a8cab 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,94 @@ "\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": 20, + "id": "aca75df0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Apples': 32, 'Oranges': 23, 'lettuce': 1}\n", + "{'Apples': 23, 'Oranges': 21, 'lettuce': 32}\n", + "That brings your total up to: 23\n", + "That brings your total up to: 44\n", + "That brings your total up to: 76\n" + ] + } + ], + "source": [ + "products = [\"Apples\", \"Oranges\", \"lettuce\"]\n", + "\n", + "\n", + "\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", + "def initialize_prices(products):\n", + " prices = {}\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = int(input(f\"Enter the price of {product}s: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Invalid price! Please enter a non-negative value.\")\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " prices[product] = price\n", + " return prices\n", + "\n", + "\n", + "\n", + "# inventory = initialize_inventory(products)\n", + "print(inventory)\n", + "#prices = initialize_prices(products)\n", + "print(prices)\n", + "\n", + "\n", + "\n", + "def get_customer_orders(inventory, prices):\n", + " total_price = 0\n", + " \n", + " for product in inventory:\n", + " valid_order = False\n", + " while not valid_order:\n", + " try:\n", + " order_quantity = int(input(f\"There are {inventory[product]} {product} for {prices[product]}€ each available- How many would you like to purchase?\"))\n", + " if order_quantity < 0:\n", + " raise ValueError(\"Invalid order Quantity! Please enter a non-negative value.\")\n", + " valid_order = True\n", + " total_price += order_quantity * prices[product]\n", + " except ValueError as error: \n", + " print(f\"Error: {error}\") \n", + " \n", + " print(f\"That brings your total up to: {total_price}\")\n", + "\n", + "\n", + "get_customer_orders(inventory, prices)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" },