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
220 changes: 216 additions & 4 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,230 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def get_unique_list(lst):\n",
" try:\n",
" if not isinstance(lst, list):\n",
" raise TypeError(\"Input must be a list.\")\n",
" \n",
" unique_list = []\n",
" for item in lst:\n",
" if item not in unique_list:\n",
" unique_list.append(item)\n",
" \n",
" except TypeError as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" else:\n",
" return unique_list\n",
" finally:\n",
" print(\"get_unique_list execution completed.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7fa7a8e3",
"metadata": {},
"outputs": [],
"source": [
"def count_case(string):\n",
" try:\n",
" if not isinstance(string, str):\n",
" raise TypeError(\"Input must be a string.\")\n",
" \n",
" upper_count = 0\n",
" lower_count = 0\n",
"\n",
" for char in string:\n",
" if char.isupper():\n",
" upper_count += 1\n",
" elif char.islower():\n",
" lower_count += 1\n",
"\n",
" except TypeError as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" else:\n",
" return (upper_count, lower_count)\n",
" finally:\n",
" print(\"count_case execution completed.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bb98ca6e",
"metadata": {},
"outputs": [],
"source": [
"import string\n",
"\n",
"def remove_punctuation(sentence):\n",
" try:\n",
" if not isinstance(sentence, str):\n",
" raise TypeError(\"Input must be a string.\")\n",
" \n",
" cleaned_sentence = \"\"\n",
" for char in sentence:\n",
" if char not in string.punctuation:\n",
" cleaned_sentence += char\n",
"\n",
" except TypeError as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" else:\n",
" return cleaned_sentence\n",
" finally:\n",
" print(\"remove_punctuation execution completed.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a1a9f66d",
"metadata": {},
"outputs": [],
"source": [
"def calculate(operator, *args):\n",
" try:\n",
" if not args:\n",
" raise ValueError(\"At least one number must be provided.\")\n",
"\n",
" for num in args:\n",
" if not isinstance(num, (int, float)):\n",
" raise TypeError(\"All operands must be numbers.\")\n",
"\n",
" if operator == \"+\":\n",
" result = sum(args)\n",
"\n",
" elif operator == \"-\":\n",
" result = args[0]\n",
" for num in args[1:]:\n",
" result -= num\n",
"\n",
" elif operator == \"*\":\n",
" result = 1\n",
" for num in args:\n",
" result *= num\n",
"\n",
" elif operator == \"/\":\n",
" if len(args) != 2:\n",
" raise ValueError(\"Division requires exactly two numbers.\")\n",
" if args[1] == 0:\n",
" raise ZeroDivisionError(\"Division by zero is not allowed.\")\n",
" result = args[0] / args[1]\n",
"\n",
" else:\n",
" raise ValueError(\"Invalid operator. Use +, -, *, /.\")\n",
"\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" else:\n",
" return result\n",
" finally:\n",
" print(\"calculate execution completed.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "10454c83",
"metadata": {},
"outputs": [],
"source": [
"def fibonacci(n):\n",
" try:\n",
" if not isinstance(n, int):\n",
" raise TypeError(\"Input must be an integer.\")\n",
" if n < 0:\n",
" raise ValueError(\"Input must be non-negative.\")\n",
"\n",
" if n <= 1:\n",
" return n\n",
" return fibonacci(n - 1) + fibonacci(n - 2)\n",
"\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" finally:\n",
" print(\"fibonacci execution completed.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f3ec0711",
"metadata": {},
"outputs": [],
"source": [
"def open_or_senior(data):\n",
" try:\n",
" if not isinstance(data, list):\n",
" raise TypeError(\"Input must be a list of [age, handicap].\")\n",
"\n",
" categories = []\n",
"\n",
" for pair in data:\n",
" if not isinstance(pair, list) or len(pair) != 2:\n",
" raise ValueError(\"Each item must be a list of two integers.\")\n",
"\n",
" age, handicap = pair\n",
"\n",
" if not isinstance(age, int) or not isinstance(handicap, int):\n",
" raise TypeError(\"Age and handicap must be integers.\")\n",
"\n",
" if age >= 55 and handicap > 7:\n",
" categories.append(\"Senior\")\n",
" else:\n",
" categories.append(\"Open\")\n",
"\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" else:\n",
" return categories\n",
" finally:\n",
" print(\"open_or_senior execution completed.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "480ee4f5",
"metadata": {},
"outputs": [],
"source": [
"def sum_of_multiples(n):\n",
" try:\n",
" if not isinstance(n, int):\n",
" raise TypeError(\"Input must be an integer.\")\n",
" if n < 0:\n",
" return 0\n",
"\n",
" total = 0\n",
" for number in range(n):\n",
" if number % 3 == 0 or number % 5 == 0:\n",
" total += number\n",
"\n",
" except Exception as e:\n",
" print(\"Error:\", e)\n",
" return None\n",
" else:\n",
" return total\n",
" finally:\n",
" print(\"sum_of_multiples execution completed.\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +278,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down