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
279 changes: 275 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,288 @@
{
"cell_type": "code",
"execution_count": null,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"id": "76e43ad7",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"# functions.py\n",
"\n",
"import string\n",
"\n",
"# 1. Returns a new list with unique elements (keeps first appearance)\n",
"def get_unique_list_f(lst):\n",
" unique = []\n",
" for item in lst:\n",
" if item not in unique:\n",
" unique.append(item)\n",
" return unique\n",
"\n",
"\n",
"# 2. Counts uppercase and lowercase letters in a string\n",
"def count_case_f(string): \n",
" upper_count = sum(1 for char in string if char.isupper())\n",
" lower_count = sum(1 for char in string if char.islower())\n",
" return upper_count, lower_count\n",
"\n",
"# 3. Removes punctuation marks from a sentence\n",
"def remove_punctuation_f(sentence):\n",
" return ''.join(char for char in sentence if char not in string.punctuation)\n",
"\n",
"# 4. Counts the number of words in a given sentence after removing punctuation\n",
"def word_count_f(sentence):\n",
" clean_sentence = remove_punctuation_f(sentence)\n",
" words = clean_sentence.split()\n",
" return len(words)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bee5a9ce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution of get_unique_list_f_safe is complete.\n",
"Unique list: [1, 2, 3, 4, 5, 6, 7, 8]\n"
]
}
],
"source": [
"# 1. Returns a new list with unique elements (keeps first appearance)\n",
"\n",
"# given this code\n",
"\n",
"def get_unique_list_f(lst):\n",
" unique = []\n",
" for item in lst:\n",
" if item not in unique:\n",
" unique.append(item)\n",
" return unique\n",
"\n",
"# Modify the code to handle possible errors in Python, \n",
"# it is recommended to use `try-except-else-finally` blocks, \n",
"# incorporate the `raise` keyword where necessary, \n",
"# and print meaningful error messages to alert users of any issues that may occur during program execution.\n",
"# ask the user to create prior list with numbers that are more time than once in the list,\n",
"# then ask the user to create an unique list from the prior list.\n",
"\n",
"def get_unique_list_f_safe(lst):\n",
" try:\n",
" if not isinstance(lst, list):\n",
" raise TypeError(\"Input must be a list.\")\n",
" unique = []\n",
" for item in lst:\n",
" if item not in unique:\n",
" unique.append(item)\n",
" except TypeError as te:\n",
" print(f\"TypeError: {te}\")\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred: {e}\")\n",
" else:\n",
" return unique\n",
" finally:\n",
" print(\"Execution of get_unique_list_f_safe is complete.\")\n",
"\n",
"# Example usage:\n",
"user_input = input(\"Please enter a list of numbers separated by commas (e.g., 1,2,2,3,4,4): \")\n",
"try:\n",
" user_list = [int(x.strip()) for x in user_input.split(',')]\n",
" unique_list = get_unique_list_f_safe(user_list)\n",
" if unique_list is not None:\n",
" print(\"Unique list:\", unique_list)\n",
"except ValueError:\n",
" print(\"Invalid input. Please ensure you enter numbers separated by commas.\")\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "0f78dc5a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution of count_case_f_safe is complete.\n",
"\n",
"Uppercase letters: 2, Lowercase letters: 8\n",
"\n",
"You entered: Hello World\n"
]
}
],
"source": [
"# 2. Counts uppercase and lowercase letters in a string\n",
"\n",
"# given this code\n",
"\n",
"def count_case_f(string): \n",
" upper_count = sum(1 for char in string if char.isupper())\n",
" lower_count = sum(1 for char in string if char.islower())\n",
" return upper_count, lower_count\n",
"\n",
"# Modify the code to handle possible errors in Python, \n",
"# it is recommended to use `try-except-else-finally` blocks, \n",
"# incorporate the `raise` keyword where necessary, \n",
"# and print meaningful error messages to alert users of any issues that may occur during program execution.\n",
"\n",
"def count_case_f_safe(string):\n",
" try:\n",
" if not isinstance(string, str):\n",
" raise TypeError(\"Input must be a string.\")\n",
" upper_count = sum(1 for char in string if char.isupper())\n",
" lower_count = sum(1 for char in string if char.islower())\n",
" except TypeError as te:\n",
" print(f\"TypeError: {te}\")\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred: {e}\")\n",
" else:\n",
" return upper_count, lower_count\n",
" finally:\n",
" print(\"Execution of count_case_f_safe is complete.\")\n",
"\n",
"# Example usage:\n",
"user_input = input(\"\\nPlease enter a sentence with mixed case letters: \")\n",
"result = count_case_f_safe(user_input)\n",
"if result is not None:\n",
" upper_count, lower_count = result\n",
" print(f\"\\nUppercase letters: {upper_count}, Lowercase letters: {lower_count}\")\n",
"\n",
"# print the sentence entered by the user\n",
" print(f\"\\nYou entered: {user_input}\")\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "4920684c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution of remove_punctuation_f_safe is complete.\n",
"\n",
"Cleaned sentence: Note This Is A Good Day Isn´t it\n",
"\n",
"You entered: Note: This Is A Good Day!!!! :) Isn´t it?\n"
]
}
],
"source": [
"# 3. Removes punctuation marks from a sentence\n",
"\n",
"# given this code\n",
"\n",
"import string\n",
"\n",
"def remove_punctuation_f(sentence):\n",
" return ''.join(char for char in sentence if char not in string.punctuation)\n",
"\n",
"# Modify the code to handle possible errors in Python, \n",
"# it is recommended to use `try-except-else-finally` blocks, \n",
"# incorporate the `raise` keyword where necessary, \n",
"# and print meaningful error messages to alert users of any issues that may occur during program execution.\n",
"\n",
"def remove_punctuation_f_safe(sentence):\n",
" try:\n",
" if not isinstance(sentence, str):\n",
" raise TypeError(\"Input must be a string.\")\n",
" cleaned_sentence = ''.join(char for char in sentence if char not in string.punctuation)\n",
" except TypeError as te:\n",
" print(f\"TypeError: {te}\")\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred: {e}\")\n",
" else:\n",
" return cleaned_sentence\n",
" finally:\n",
" print(\"Execution of remove_punctuation_f_safe is complete.\")\n",
"\n",
"# Example usage:\n",
"user_input = input(\"\\nPlease enter a sentence with punctuation marks: \")\n",
"cleaned_sentence = remove_punctuation_f_safe(user_input)\n",
"if cleaned_sentence is not None:\n",
" print(f\"\\nCleaned sentence: {cleaned_sentence}\")\n",
" print(f\"\\nYou entered: {user_input}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "0ed545f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution of remove_punctuation_f_safe is complete.\n",
"Execution of word_count_f_safe is complete.\n",
"\n",
"Number of words: 9\n",
"\n",
"You entered: Hello World!!! This Is A Good Day!!! :) Isn´t It?\n"
]
}
],
"source": [
"# 4. Counts the number of words in a given sentence after removing punctuation\n",
"\n",
"# given this code\n",
"\n",
"import string\n",
"\n",
"def word_count_f(sentence):\n",
" clean_sentence = remove_punctuation_f(sentence)\n",
" words = clean_sentence.split()\n",
" return len(words)\n",
"\n",
"# Modify the code to handle possible errors in Python, \n",
"# it is recommended to use `try-except-else-finally` blocks, \n",
"# incorporate the `raise` keyword where necessary, \n",
"# and print meaningful error messages to alert users of any issues that may occur during program execution.\n",
"def word_count_f_safe(sentence):\n",
" try:\n",
" if not isinstance(sentence, str):\n",
" raise TypeError(\"Input must be a string.\")\n",
" clean_sentence = remove_punctuation_f_safe(sentence)\n",
" if clean_sentence is None:\n",
" raise ValueError(\"Failed to remove punctuation from the sentence.\")\n",
" words = clean_sentence.split()\n",
" word_count = len(words)\n",
" except TypeError as te:\n",
" print(f\"TypeError: {te}\")\n",
" except ValueError as ve:\n",
" print(f\"ValueError: {ve}\")\n",
" except Exception as e:\n",
" print(f\"An unexpected error occurred: {e}\")\n",
" else:\n",
" return word_count\n",
" finally:\n",
" print(\"Execution of word_count_f_safe is complete.\")\n",
"\n",
"# Example usage:\n",
"user_input = input(\"\\nPlease enter a sentence to count the number of words: \")\n",
"word_count = word_count_f_safe(user_input)\n",
"if word_count is not None:\n",
" print(f\"\\nNumber of words: {word_count}\")\n",
" print(f\"\\nYou entered: {user_input}\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +337,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down