From 94f1b0da77030a535afe6d2815ecc68f82314be6 Mon Sep 17 00:00:00 2001 From: Hoai Thuong TRAN Date: Tue, 30 Dec 2025 02:59:40 +0100 Subject: [PATCH] Solved --- lab-python-error-handling.ipynb | 122 +++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..e858d9a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,126 @@ "\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": "13f2e510-bab7-4ea3-bf6f-ddbbb9126af5", + "metadata": {}, + "outputs": [], + "source": [ + "# 2. Modify the calculate_total_price function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "55a0a240-fe78-43ec-9b47-9d7f15110e75", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(products):\n", + " total_price = 0\n", + "\n", + " for product in products:\n", + " valid_price = False\n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " total_price += price\n", + " valid_price = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}. Please enter a valid price.\")\n", + "\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce3addee-b910-4404-a3d2-b71de698cdc5", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Modify the get_customer_orders function to include error handling." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "09613aa1-3ec4-4a58-9f89-d1d92a8ee543", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " orders = []\n", + "\n", + " # Nhập số lượng đơn hàng\n", + " valid_number = False\n", + " while not valid_number:\n", + " try:\n", + " num_orders = int(input(\"How many products do you want to order? \"))\n", + " if num_orders <= 0:\n", + " raise ValueError(\"Number of orders must be positive.\")\n", + " valid_number = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " # Nhập từng sản phẩm\n", + " for i in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(f\"Enter product name #{i+1}: \")\n", + "\n", + " if product not in inventory:\n", + " raise KeyError(\"Product does not exist.\")\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + "\n", + " orders.append(product)\n", + " inventory[product] -= 1\n", + " valid_product = True\n", + "\n", + " except KeyError as error:\n", + " print(f\"Error: {error} Please enter a valid product name.\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return orders" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf27bd50-be57-4369-93bb-60731677d8f6", + "metadata": {}, + "outputs": [], + "source": [ + "# 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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1928f35a-0897-44da-b496-8f4ab979fcd0", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"apple\", \"banana\", \"orange\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "print(\"Inventory:\", inventory)\n", + "\n", + "orders = get_customer_orders(inventory)\n", + "print(\"Customer orders:\", orders)\n", + "\n", + "total = calculate_total_price(orders)\n", + "print(\"Total price:\", total)" + ] } ], "metadata": { @@ -90,7 +210,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,