From e0157ad7a026ef613797fef868ef09fa7426d3e0 Mon Sep 17 00:00:00 2001 From: Pedro Manuel Gil Basilio Ferreira Date: Sun, 30 Nov 2025 09:35:50 +0000 Subject: [PATCH] Labsolved --- lab-python-error-handling.ipynb | 210 +++++++++++++++++++++++++++++++- 1 file changed, 207 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..cf78343 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,217 @@ "\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": 1, + "id": "47b226c1-c908-4f08-9cc7-e70d6e2f1ed8", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Put the pice for banana: 4\n", + "Put the pice for apple: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid entry. Please enter a numerc value.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Put the pice for apple: 5\n", + "Put the pice for pear: 6\n" + ] + }, + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = ['banana', 'apple', 'pear']\n", + "\n", + "def calculate_total_price(products):\n", + " totalprice = 0\n", + " for product in products:\n", + " validprice = False\n", + " while not validprice:\n", + " try:\n", + " price = float(input(f\"Put the pice for {product}: \"))\n", + " if price >= 0:\n", + " totalprice += price\n", + " validprice = True\n", + " else:\n", + " print(\"Error: enter a non-negative price.\")\n", + " except ValueError:\n", + " print(\"Invalid entry. Please enter a numerc value.\")\n", + " return int(totalprice)\n", + "\n", + "calculate_total_price(products)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a26c9497-d9e1-4754-a118-e410a0f2ac22", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Number of orders: 3\n", + "Produto #1: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid produtc\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product #1: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid produtc\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product #1: r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid produtc\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product #1: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid produtc\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product #1: banana\n", + "Quantity of banana: 2\n", + "Produto #2: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid produtc\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Product #2: banana\n", + "Quantity of banana: 4\n", + "Produto #3: apple\n", + "Quantity of apple: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'banana': 4, 'apple': 4}\n" + ] + } + ], + "source": [ + "inventory = {\"banana\": 10, \"apple\": 5, \"pear\": 8} \n", + "\n", + "orders = get_customer_orders(inventory)\n", + "print(orders)\n", + "\n", + "def get_customer_orders(inventory):\n", + " orders = {}\n", + "\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Number of orders: \"))\n", + " if num_orders > 0:\n", + " break\n", + " print(\"Enter a positive number.\")\n", + " except ValueError:\n", + " print(\"Yu have to enter an integer number.\")\n", + " for i in range(num_orders):\n", + " product = input(f\"Produto #{i+1}: \").strip().lower()\n", + " while product not in inventory or inventory[product] <= 0:\n", + " print(\"Invalid produtc\")\n", + " product = input(f\"Product #{i+1}: \").strip().lower()\n", + "\n", + " while True:\n", + " try:\n", + " qty = int(input(f\"Quantity of {product}: \"))\n", + " if 0 < qty <= inventory[product]:\n", + " break\n", + " print(\"Invalid quantity.\")\n", + " except ValueError:\n", + " print(\"You have to enter an integer number.\")\n", + "\n", + " orders[product] = qty\n", + "\n", + " return orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df12b8c5-5d8e-4d6b-9eee-0f56c6099037", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +294,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,