Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 196 additions & 5 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,211 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"import string\n",
"\n",
"\n",
"# 1. Return the unique values from a list\n",
"def get_unique_list_f(values):\n",
" try:\n",
" if not isinstance(values, list):\n",
" raise TypeError(\"The input must be a list.\")\n",
"\n",
" unique_values = list(set(values))\n",
"\n",
" except TypeError as error:\n",
" print(f\"Error: {error}\")\n",
" return None\n",
"\n",
" else:\n",
" print(\"Unique list created successfully.\")\n",
" return unique_values\n",
"\n",
" finally:\n",
" print(\"Finished running get_unique_list_f().\")\n",
"\n",
"\n",
"# 2. Count uppercase and lowercase letters\n",
"def count_case_f(text):\n",
" try:\n",
" if not isinstance(text, str):\n",
" raise TypeError(\"The input must be a string.\")\n",
"\n",
" uppercase_count = sum(1 for character in text if character.isupper())\n",
" lowercase_count = sum(1 for character in text if character.islower())\n",
"\n",
" except TypeError as error:\n",
" print(f\"Error: {error}\")\n",
" return None\n",
"\n",
" else:\n",
" print(\"Letter cases counted successfully.\")\n",
" return uppercase_count, lowercase_count\n",
"\n",
" finally:\n",
" print(\"Finished running count_case_f().\")\n",
"\n",
"\n",
"# 3. Remove punctuation from text\n",
"def remove_punctuation_f(text):\n",
" try:\n",
" if not isinstance(text, str):\n",
" raise TypeError(\"The input must be a string.\")\n",
"\n",
" cleaned_text = \"\".join(\n",
" character for character in text\n",
" if character not in string.punctuation\n",
" )\n",
"\n",
" except TypeError as error:\n",
" print(f\"Error: {error}\")\n",
" return None\n",
"\n",
" else:\n",
" print(\"Punctuation removed successfully.\")\n",
" return cleaned_text\n",
"\n",
" finally:\n",
" print(\"Finished running remove_punctuation_f().\")\n",
"\n",
"\n",
"# 4. Count the words in text\n",
"def word_count_f(text):\n",
" try:\n",
" if not isinstance(text, str):\n",
" raise TypeError(\"The input must be a string.\")\n",
"\n",
" if not text.strip():\n",
" raise ValueError(\"The text cannot be empty.\")\n",
"\n",
" cleaned_text = remove_punctuation_f(text)\n",
"\n",
" if cleaned_text is None:\n",
" raise ValueError(\"The text could not be cleaned.\")\n",
"\n",
" number_of_words = len(cleaned_text.split())\n",
"\n",
" except (TypeError, ValueError) as error:\n",
" print(f\"Error: {error}\")\n",
" return None\n",
"\n",
" else:\n",
" print(\"Words counted successfully.\")\n",
" return number_of_words\n",
"\n",
" finally:\n",
" print(\"Finished running word_count_f().\")\n",
"\n",
"\n",
"# 5. Perform a calculation\n",
"def calculate(number_1, number_2, operator):\n",
" try:\n",
" if not isinstance(number_1, (int, float)):\n",
" raise TypeError(\"The first value must be a number.\")\n",
"\n",
" if not isinstance(number_2, (int, float)):\n",
" raise TypeError(\"The second value must be a number.\")\n",
"\n",
" if operator == \"+\":\n",
" result = number_1 + number_2\n",
" elif operator == \"-\":\n",
" result = number_1 - number_2\n",
" elif operator == \"*\":\n",
" result = number_1 * number_2\n",
" elif operator == \"/\":\n",
" if number_2 == 0:\n",
" raise ZeroDivisionError(\"A number cannot be divided by zero.\")\n",
" result = number_1 / number_2\n",
" else:\n",
" raise ValueError(\"The operator must be +, -, * or /.\")\n",
"\n",
" except (TypeError, ValueError, ZeroDivisionError) as error:\n",
" print(f\"Error: {error}\")\n",
" return None\n",
"\n",
" else:\n",
" print(\"Calculation completed successfully.\")\n",
" return result\n",
"\n",
" finally:\n",
" print(\"Finished running calculate().\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1858d663-811c-4065-a9a5-f4bc1e40c814",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unique list created successfully.\n",
"Finished running get_unique_list_f().\n",
"[1, 2, 3, 4]\n",
"\n",
"Letter cases counted successfully.\n",
"Finished running count_case_f().\n",
"(2, 8)\n",
"\n",
"Punctuation removed successfully.\n",
"Finished running remove_punctuation_f().\n",
"Hello world\n",
"\n",
"Punctuation removed successfully.\n",
"Finished running remove_punctuation_f().\n",
"Words counted successfully.\n",
"Finished running word_count_f().\n",
"5\n",
"\n",
"Calculation completed successfully.\n",
"Finished running calculate().\n",
"5.0\n",
"\n",
"Error: A number cannot be divided by zero.\n",
"Finished running calculate().\n",
"None\n",
"\n",
"Error: The operator must be +, -, * or /.\n",
"Finished running calculate().\n",
"None\n"
]
}
],
"source": [
"print(get_unique_list_f([1, 2, 2, 3, 4, 4]))\n",
"print()\n",
"\n",
"print(count_case_f(\"Hello World\"))\n",
"print()\n",
"\n",
"print(remove_punctuation_f(\"Hello, world!\"))\n",
"print()\n",
"\n",
"print(word_count_f(\"Hello, world! Welcome to Python.\"))\n",
"print()\n",
"\n",
"print(calculate(10, 2, \"/\"))\n",
"print()\n",
"\n",
"# Tests that intentionally produce handled errors\n",
"print(calculate(10, 0, \"/\"))\n",
"print()\n",
"\n",
"print(calculate(10, 5, \"%\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:anaconda3] *",
"language": "python",
"name": "python3"
"name": "conda-env-anaconda3-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -66,7 +257,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down