diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..be7fa13 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,144 @@ "\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": 10, + "id": "3936290e", + "metadata": {}, + "outputs": [], + "source": [ + "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": null, + "id": "c745f8ac", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " customer_orders = []\n", + " \n", + " while True:\n", + " product = input(\"Enter product name (or type 'done' to finish): \")\n", + "\n", + " if product.lower() == \"done\":\n", + " break\n", + "\n", + " if product not in inventory:\n", + " print(\"Product does not exist. Try again.\")\n", + " elif inventory[product] == 0:\n", + " print(\"Product is out of stock.\")\n", + " else:\n", + " customer_orders.append(product)\n", + " inventory[product] -= 1\n", + " print(product + \" added to the order.\")\n", + "\n", + " return customer_orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "956c322d", + "metadata": {}, + "outputs": [], + "source": [ + "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 = int(input(f\"Enter price for {product}: \"))\n", + "\n", + " if price < 0:\n", + " print(\"Price cannot be negative.\")\n", + " else:\n", + " total_price += price\n", + " valid_price = True\n", + "\n", + " except ValueError:\n", + " print(\"Please enter a valid number.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "52839e7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Product does not exist. try again\n", + "Product added.\n", + "Customer ordered: {'apple'}\n", + "Total price: 2\n", + "Updated inventory: {'apple': 2, 'banana': 4, 'orange': 1, 'grapes': 2}\n" + ] + } + ], + "source": [ + "products = [\"apple\", \"banana\", \"orange\", \"grapes\"]\n", + "\n", + "# Initialize inventory\n", + "inventory = initialize_inventory(products)\n", + "\n", + "# Get orders\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "# Calculate total price\n", + "total = calculate_total_price(customer_orders)\n", + "\n", + "# Print results\n", + "print(\"Customer ordered:\", customer_orders)\n", + "print(\"Total price:\", total)\n", + "print(\"Updated inventory:\", inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86d2753a", + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +223,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,