From 3aba3012de9f4c65dd63e667411b17821d83e310 Mon Sep 17 00:00:00 2001 From: Helena Krenzlin Date: Sat, 24 Jan 2026 15:04:41 +0100 Subject: [PATCH] Weeklab2 done --- lab-python-error-handling.ipynb | 131 +++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..ce51905 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,138 @@ "\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": "c591d088", + "metadata": {}, + "outputs": [], + "source": [ + "# from previous exercise\n", + "def calculate_total_price (customer_orders):\n", + " total = sum([float(input(f\"Enter price for {product}: \")) for product in customer_orders])\n", + " return total\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "338b29b8", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "aea7f035", + "metadata": {}, + "outputs": [], + "source": [ + "customer_orders = set()\n", + "def get_customer_orders():\n", + " customer_orders = set()\n", + " continuing_order =\"yes\"\n", + " while continuing_order==\"yes\":\n", + " product = (input(f\"Enter a product name:\"))\n", + " customer_orders.add(product)\n", + " continuing_order = input (f\"Add another?( yes/no):\")\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "2a565ccb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'book', 'mug', 't-shirt'}\n" + ] + } + ], + "source": [ + "print (get_customer_orders())" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "6cc6c22c", + "metadata": {}, + "outputs": [], + "source": [ + "#2\n", + "def calculate_total_price(get_customer_orders):\n", + " total = 0 \n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price for {customer_orders}: \"))\n", + " if price >= 0:\n", + " total += price\n", + " valid_input = True\n", + " else:\n", + " print (\"Price cannot be negative. Please enter a valid price.\")\n", + " except ValueError:\n", + " print (\"Invalid input. Please enter a numeric value\")\n", + " finally:\n", + " print (\"Operation complete\")\n", + " return total " + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "931ba608", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "calculate_total_price() missing 1 required positional argument: 'get_customer_orders'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[42]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mcalculate_total_price\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[31mTypeError\u001b[39m: calculate_total_price() missing 1 required positional argument: 'get_customer_orders'" + ] + } + ], + "source": [ + "calculate_total_price()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "29217fb2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " The final amount to pay is:0 €\n" + ] + } + ], + "source": [ + "grand_total = calculate_total_price(customer_orders)\n", + "print(f\" The final amount to pay is:{grand_total} €\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +217,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,