From 3c1081f4d016a72faebbd66d1961478e3a3fc677 Mon Sep 17 00:00:00 2001 From: Feliciu Svabu Date: Fri, 27 Feb 2026 15:27:01 +0000 Subject: [PATCH] finish extra lab --- lab-python-error-handling.ipynb | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..0befa3e 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -42,17 +42,40 @@ { "cell_type": "code", "execution_count": null, - "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", + "id": "d9b39448", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "a\n", + "Warning: Mixed types (int vs str)\n", + "20\n" + ] + } + ], "source": [ - "# your code goes here" + "def smaller(a, b):\n", + " try:\n", + " # 1. Try to return the actual smaller value\n", + " return a if a < b else b\n", + " except TypeError:\n", + " # 2. If it fails, convert both to strings and compare alphabetically\n", + " print(f\"Warning: Mixed types ({type(a).__name__} vs {type(b).__name__})\")\n", + " return str(a) if str(a) < str(b) else str(b)\n", + "\n", + "\n", + "print(smaller(10, 5)) # Normal math: Returns 10\n", + "print(smaller(\"z\", \"a\")) # Normal string: Returns \"z\"\n", + "print(smaller(5, \"20\")) # Mixed types: Returns '5' (because \"5\" > \"2\" alphabetically!)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -66,7 +89,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,