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
154 changes: 151 additions & 3 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,161 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def get_unique_list_f(lst):\n",
" \"\"\"\n",
" Takes a list as an argument and returns a new list with unique elements from the first list.\n",
"\n",
" Parameters:\n",
" lst (list): The input list.\n",
"\n",
" Returns:\n",
" list: A new list with unique elements from the input list.\n",
" \"\"\"\n",
" try:\n",
" new_list = list(set(lst))\n",
" except TypeError as e:\n",
" print(f'Error: {e}')\n",
" else:\n",
" return new_list\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "4502d432",
"metadata": {},
"outputs": [],
"source": [
"def count_case_f(string):\n",
" \"\"\"\n",
" Returns the number of uppercase and lowercase letters in the given string.\n",
"\n",
" Parameters:\n",
" string (str): The string to count uppercase and lowercase letters in.\n",
"\n",
" Returns:\n",
" A tuple containing the count of uppercase and lowercase letters in the string.\n",
" \"\"\"\n",
" \n",
" \n",
" upper_gen = sum(1 for char in string if char.isupper()) \n",
" lower_gen = sum(1 for char in string if char.islower())\n",
" \n",
" return ({'Uppercase count':upper_gen},{'Lowercase count':lower_gen})\n"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "d37d63c4",
"metadata": {},
"outputs": [],
"source": [
"def remove_punctuation_f(sentence):\n",
" \"\"\"\n",
" Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence.\n",
"\n",
" Parameters:\n",
" sentence (str): A string representing a sentence.\n",
"\n",
" Returns:\n",
" str: The sentence without any punctuation marks.\n",
" \"\"\"\n",
" try:\n",
" punctuation = ';:!?,.\"\\''\n",
" for mark in punctuation:\n",
" sentence = sentence.replace(mark,'')\n",
" except AttributeError as e:\n",
" print(f'Error: {e}')\n",
" print('Input has to be a string')\n",
" else:\n",
" return sentence\n",
"\n",
" \n",
" \n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "524ef67a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: 'list' object has no attribute 'replace'\n",
"Input has to be a string\n"
]
}
],
"source": [
"remove_punctuation_f([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "66d27873",
"metadata": {},
"outputs": [],
"source": [
"def word_count_f(sentence):\n",
" \"\"\"\n",
" Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence.\n",
" Note: A word is defined as a sequence of characters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence.\n",
" \n",
" Parameters:\n",
" sentence (str): A string representing a sentence.\n",
"\n",
" Returns:\n",
" int: The number of words in the sentence.\n",
" \"\"\"\n",
" import string # Quitamos los símbolos usando el \"catálogo\" de string.punctuation\n",
" # Esto es un generador. Es lo mismo que un lista pero este no almacena los datos. §\n",
" # Cuando lo usa lo elimina, por lo que apenas ocupa memoria. \n",
" try:\n",
" lenght_sentence = len(''.join(char for char in sentence if char not in string.punctuation).split())\n",
" except TypeError as e:\n",
" print(f'Error; {e}')\n",
" else:\n",
" return lenght_sentence"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "da7e7929",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error; 'in <string>' requires string as left operand, not int\n"
]
}
],
"source": [
"word_count_f([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36c7e267",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +214,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down