From 949f184290f3c0618266c1e20c25f3af5dce100c Mon Sep 17 00:00:00 2001 From: Francisca Date: Thu, 19 Feb 2026 17:07:03 +0000 Subject: [PATCH] Error handling extra --- ...lab-python-error-handling-checkpoint.ipynb | 74 +++++++ lab-python-error-handling.ipynb | 208 +++++++++++++++++- 2 files changed, 278 insertions(+), 4 deletions(-) create mode 100644 .ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb diff --git a/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb new file mode 100644 index 0000000..3e50ef8 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-error-handling-checkpoint.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | Error Handling" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to identify, handle and recover from potential errors in Python code using try-except blocks." + ] + }, + { + "cell_type": "markdown", + "id": "e253e768-aed8-4791-a800-87add1204afa", + "metadata": {}, + "source": [ + "## Challenge \n", + "\n", + "Paste here your lab *functions* solutions. Apply error handling techniques to each function using try-except blocks. " + ] + }, + { + "cell_type": "markdown", + "id": "9180ff86-c3fe-4152-a609-081a287fa1af", + "metadata": {}, + "source": [ + "The try-except block in Python is designed to handle exceptions and provide a fallback mechanism when code encounters errors. By enclosing the code that could potentially throw errors in a try block, followed by specific or general exception handling in the except block, we can gracefully recover from errors and continue program execution.\n", + "\n", + "However, there may be cases where an input may not produce an immediate error, but still needs to be addressed. In such situations, it can be useful to explicitly raise an error using the \"raise\" keyword, either to draw attention to the issue or handle it elsewhere in the program.\n", + "\n", + "Modify the code to handle possible errors in Python, it is recommended to use `try-except-else-finally` blocks, incorporate the `raise` keyword where necessary, and print meaningful error messages to alert users of any issues that may occur during program execution.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..fab4352 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,12 +41,212 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The value of i is -2 and the inverse of i is -0.5\n", + "-0.5\n", + "The value of i is -1 and the inverse of i is -1.0\n", + "-1.0\n", + "The value of i is 0, cannot divide by zero!\n", + "The value of i is 1 and the inverse of i is 1.0\n", + "1.0\n", + "The value of i is 2 and the inverse of i is 0.5\n", + "0.5\n", + "The value of i is 3 and the inverse of i is 0.3333333333333333\n", + "0.3333333333333333\n", + "The value of i is 4 and the inverse of i is 0.25\n", + "0.25\n" + ] + } + ], "source": [ - "# your code goes here" + "for i in range(-2, 5): \n", + " try:\n", + " inverse = 1 / i\n", + " print(f\"The value of i is {i} and the inverse of i is {1/i}\")\n", + " print(inverse)\n", + " except ZeroDivisionError:\n", + " print(f\"The value of i is {i}, cannot divide by zero!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "4fd42b6b-3743-4cd0-b6db-278a28652483", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The ratio (1/i) is -0.5\n", + "The ratio (1/i) is -1.0\n", + "Error for i = 0: Why do you want to divide by 0?\n", + "The ratio (1/i) is 1.0\n", + "The ratio (1/i) is 0.5\n", + "The ratio (1/i) is 0.3333333333333333\n", + "The ratio (1/i) is 0.25\n", + "The ratio (1/i) is 0.2\n", + "The ratio (1/i) is 0.16666666666666666\n", + "The ratio (1/i) is 0.14285714285714285\n", + "The ratio (1/i) is 0.125\n", + "The ratio (1/i) is 0.1111111111111111\n", + "The ratio (1/i) is 0.1\n", + "The ratio (1/i) is 0.09090909090909091\n", + "The ratio (1/i) is 0.08333333333333333\n", + "The ratio (1/i) is 0.07692307692307693\n", + "The ratio (1/i) is 0.07142857142857142\n" + ] + } + ], + "source": [ + "for i in range(-2, 15):\n", + " try:\n", + " if i == 0:\n", + " raise ZeroDivisionError(\"Why do you want to divide by 0?\")\n", + " else:\n", + " ratio = 1 / i\n", + " print(f\"The ratio (1/i) is {ratio}\")\n", + " \n", + " except ZeroDivisionError as e:\n", + " print(f\"Error for i = {i}: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "9e04bfce-c147-4a8b-92a8-8e988a1c5c7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HELLO, WORLD!\n", + "Error: 'str' object has no attribute 'age'\n" + ] + } + ], + "source": [ + "text = \"Hello, world!\"\n", + "print(text.upper()) \n", + " \n", + "\n", + "try:\n", + " print(text.age)\n", + "except AttributeError as e:\n", + " print(f\"Error: {e}\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "f06a74d6-137b-4052-bb64-6e31be36db2c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "Error: list index out of range\n" + ] + } + ], + "source": [ + "numbers = [1, 2, 3]\n", + "print(numbers[2]) \n", + "\n", + "try:\n", + " print(numbers[5]) \n", + "except IndexError as e:\n", + " print(f\"Error: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1163fa6f-e345-4c80-a496-fbd67321e1a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Alice\n", + "Error: 'gender'\n" + ] + } + ], + "source": [ + "person = {\"name\": \"Alice\", \"age\": 30}\n", + "print(person[\"name\"]) \n", + "\n", + "try:\n", + " print(person[\"gender\"]) \n", + "except KeyError as e:\n", + " print(f\"Error: {e}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "8e43d0d3-e39b-458f-9b59-03e3818a635e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: unsupported operand type(s) for +: 'int' and 'str'\n" + ] + } + ], + "source": [ + "num1 = 5\n", + "num2 = \"10\"\n", + "\n", + "try:\n", + " sum_result = num1 + num2 \n", + " print(f\"The sum is: {sum_result}\")\n", + "except TypeError as e:\n", + " print(f\"Error: {e}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f19d2190-532d-45be-ba54-af126876afb0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123\n", + "Error: invalid literal for int() with base 10: 'abc'\n" + ] + } + ], + "source": [ + "num_str = \"123\"\n", + "num_int = int(num_str) \n", + "print(num_int) \n", + "\n", + "invalid_str = \"abc\"\n", + "\n", + "try:\n", + " invalid_int = int(invalid_str)\n", + " print(f\"The integer is {invalid_str}\")\n", + "except ValueError as e:\n", + " print(f\"Error: {e}\")" ] } ], @@ -66,7 +266,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,