From 6ebe2a43be4107261bcedc8d10f651fcc0fd44d5 Mon Sep 17 00:00:00 2001 From: nadiyashahaibi-DA Date: Mon, 27 Jul 2026 18:58:50 +0100 Subject: [PATCH] Completed lab --- lab-python-error-handling.ipynb | 151 +++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c2dffee 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,155 @@ "\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": "bf8b5a1b-f8fe-4884-8459-304d3c92fa23", + "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": "0fd055ac-4edf-4107-93d1-4d672c4b5c73", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b31d543a-7fa9-4058-a73f-3f3385133d83", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_products):\n", + " total_price = 0\n", + "\n", + " for product in customer_orders:\n", + " valid_input = False\n", + "\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price >= 0:\n", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric price.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1164004c-a3a4-4710-961c-ff4b84769873", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a61dc37-649b-44f4-ac6f-9cd4911ee8ba", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + "\n", + " while True:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer order: \"))\n", + "\n", + " if number_of_orders < 0:\n", + " print(\"Error: number of orders cannot be negative. Please try again.\")\n", + " break\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a whole number. \")\n", + "\n", + " customer_orders = []\n", + " \n", + " for i in range(number_of_orders):\n", + " valid_product = False\n", + "\n", + " while not valid_product:\n", + " product_name = input(\"Enter the product name to order: \").lower()\n", + "\n", + " if product_name in inventory and inventory [product_name] > 0:\n", + " customer_orders.append(product_name)\n", + " valid_product = True\n", + " elif product_name not in inventory:\n", + " print(\"That product is not is inventory. Please enter a valid product name. \")\n", + " else:\n", + " print(\"That product is out of stock. Please choose another product.\")\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0264d0c2-113b-41ea-87d0-0cfb027349ad", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f2a39d9-495f-4528-ae77-daeeb83e47dc", + "metadata": {}, + "outputs": [], + "source": [ + "if __name__ == \"__main__\":\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " product_prices = {\n", + " \"t-shirt\": 15,\n", + " \"mug\": 8,\n", + " \"hat\": 12,\n", + " \"book\" : 20,\n", + " \"keychain\": 5\n", + " }\n", + "\n", + " print(\"initialize Inventory\")\n", + " inventory = initialize_inventory(products)\n", + "\n", + " print(\"Get Customer Orders\")\n", + " customer_orders = get_customer_orders(inventory)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "868a795e-39e2-4766-82af-673fdcfc3642", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -90,7 +239,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,