diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..b43ca1b --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,239 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Before your start:\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Libraries\n", + "import math" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Handling Errors Using `if` Statements\n", + "\n", + "In many cases, we are able to identify issues that may come up in our code and handle those handlful of issues with an `if` statment. Sometimes we would like to handle different types of inputs and are aware that later in the code, we will have to write two different branches of code for the two different cases we allowed in the beginning.\n", + "\n", + "In the 3 cells below, add an `if` statment that will handle both types of input allowed in the functions." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You can't find the square root of a negative number. Try again.\n" + ] + } + ], + "source": [ + "# Modify the code below to handle positive and negative numbers by adding an if statement and performing a transformation:\n", + "\n", + "def sqrt_for_all(x):\n", + " if int(x) >= 0:\n", + " return math.sqrt(x)\n", + " print(\"The square root of your number is \" + str(sqrt_for_all))\n", + " \n", + " else:\n", + " print(\"You can't find the square root of a negative number. Try again.\")\n", + " \n", + "sqrt_for_all(-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dividing a number by 0 is 0, why are you doing this?\n" + ] + } + ], + "source": [ + "# Modify the code below to handle zero as well. In the case of zero, return zero\n", + "\n", + "def divide(x, y):\n", + " if int(y) > 0:\n", + " return x / y\n", + " print(\"The division of these two numbers is \")\n", + " else:\n", + " print(\"Dividing a number by 0 is 0, why are you doing this?\")\n", + "\n", + "divide(5, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'int' object is not iterable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [19]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 21\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 22\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m [a \u001b[38;5;241m+\u001b[39m element \u001b[38;5;28;01mfor\u001b[39;00m element \u001b[38;5;129;01min\u001b[39;00m l]\n\u001b[1;32m---> 24\u001b[0m \u001b[43madd_elements\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m6\u001b[39;49m\u001b[43m)\u001b[49m\n", + "Input \u001b[1;32mIn [19]\u001b[0m, in \u001b[0;36madd_elements\u001b[1;34m(a, l)\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msum\u001b[39m(a,l)\n\u001b[0;32m 21\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m---> 22\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m [a \u001b[38;5;241m+\u001b[39m element \u001b[38;5;28;01mfor\u001b[39;00m element \u001b[38;5;129;01min\u001b[39;00m l]\n", + "\u001b[1;31mTypeError\u001b[0m: 'int' object is not iterable" + ] + } + ], + "source": [ + "# Modify the function below that it will take either an number and a list or two numbers. \n", + "# If we take two numbers, add them together and return a list of length 1. \n", + "# Otherwise, add the number to every element of the list and return the resulting list\n", + "\n", + "def add_elements(a, l):\n", + " \"\"\"\n", + " This function takes either two numbers or a list and a number \n", + " and adds the number to all elements of the list.\n", + " If the function only takes two numbers, it returns a list \n", + " of length one that is the sum of the numbers.\n", + " Input: number and list or two numbers\n", + " Output: list\n", + " \n", + " Sample Input: 5, 6\n", + " Sample Output: [11]\n", + " \"\"\"\n", + " \n", + " return [a + element for element in l]\n", + " \n", + "add_elements(5, 6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Fixing Errors to Get Code to Run\n", + "\n", + "Sometimes the error is not caused by the input but by the code itself. In the 2 following cells below, examine the error and correct the code to avoid the error." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Modify the code below:\n", + "\n", + "l = [1,2,3,4]\n", + "\n", + "sum([element + 1 for element in l]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Modify the code below:\n", + "\n", + "l = [1,2,3,4]\n", + "\n", + "for element in l:\n", + " print(\"The current element in the loop is\" + element)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Raise Errors on Your Own\n", + "\n", + "There are cases where you need to alert your users of a problem even if the input will not immediately produce an error. In these cases you may want to throw an error yourself to bring attention to the problem. In the 2 cells below, write the functions as directed and add the appropriate errors using the `raise` clause. Make sure to add a meaningful error message." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def log_square(x):\n", + " \"\"\"\n", + " This function takes a numeric value and returns the \n", + " natural log of the square of the number.\n", + " The function raises an error if the number is equal to zero.\n", + " Use the math.log function in this funtion.\n", + " \n", + " Input: Real number\n", + " Output: Real number or error\n", + " \n", + " Sample Input: 5\n", + " Sample Output: 3.21887\n", + " \"\"\"\n", + " \n", + " # Your code here:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def check_capital(x):\n", + " \"\"\"\n", + " This function returns true if the string contains \n", + " at least one capital letter and throws an error otherwise.\n", + " \n", + " Input: String\n", + " Output: Bool or error message\n", + " \n", + " Sample Input: 'John'\n", + " Sample Output: True\n", + " \"\"\"\n", + " \n", + " # Your code 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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 2a08c31..7ae886b 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,82 +33,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You can't find the square root of a negative number. Try again.\n" + ] + } + ], "source": [ "# Modify the code below to handle positive and negative numbers by adding an if statement and performing a transformation:\n", "\n", "def sqrt_for_all(x):\n", - " \"\"\"\n", - " This function will take any real number and \n", - " return the square root of its magnitude.\n", + " if int(x) >= 0:\n", + " return math.sqrt(x)\n", + " print(\"The square root of your number is \" + str(sqrt_for_all))\n", " \n", - " Input: Real number\n", - " Output: Real number\n", - " \n", - " Sample Input: -4\n", - " Sample Output: 2.0\n", - " \"\"\"\n", - " \n", - " return math.sqrt(x)\n", - "\n", + " else:\n", + " print(\"You can't find the square root of a negative number. Try again.\")\n", + " \n", "sqrt_for_all(-1)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dividing a number by 0 is 0, why are you doing this?\n" + ] + } + ], "source": [ "# Modify the code below to handle zero as well. In the case of zero, return zero\n", "\n", "def divide(x, y):\n", - " \"\"\"\n", - " This function will take any two real numbers \n", - " and return their quotient. \n", - " If the denominator is zero, we return zero.\n", - " \n", - " Input: Real number\n", - " Output: Real number\n", - " \n", - " Sample Input: 5, 1\n", - " Sample Output: 5.0\n", - " \"\"\"\n", - " \n", - " return x / y\n", + " if int(y) > 0:\n", + " return x / y\n", + " print(\"The division of these two numbers is \")\n", + " else:\n", + " print(\"Dividing a number by 0 is 0, why are you doing this?\")\n", "\n", "divide(5, 0)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[11]\n", + "[6, 7]\n" + ] + } + ], "source": [ "# Modify the function below that it will take either an number and a list or two numbers. \n", "# If we take two numbers, add them together and return a list of length 1. \n", "# Otherwise, add the number to every element of the list and return the resulting list\n", "\n", "def add_elements(a, l):\n", - " \"\"\"\n", - " This function takes either two numbers or a list and a number \n", - " and adds the number to all elements of the list.\n", - " If the function only takes two numbers, it returns a list \n", - " of length one that is the sum of the numbers.\n", - " \n", - " Input: number and list or two numbers\n", - " Output: list\n", - " \n", - " Sample Input: 5, 6\n", - " Sample Output: [11]\n", - " \"\"\"\n", - " \n", - " return [a + element for element in l]\n", + " if type(l) == int:\n", + " return [ a + l ]\n", + " else:\n", + " return [ a + element for element in l]\n", " \n", - "add_elements(5, 6)" + "print(add_elements(5, 6))\n", + "print(add_elements(5, [1,2]))" ] }, { @@ -122,29 +124,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Modify the code below:\n", "\n", "l = [1,2,3,4]\n", "\n", - "sum([element + 1 for element in l]" + "sum([element for element in l]) #so it can do 1+2=3 // 3+3 = 6 // 6+4 = 10 " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The current element in the loop is 1\n", + "The current element in the loop is 2\n", + "The current element in the loop is 3\n", + "The current element in the loop is 4\n" + ] + } + ], "source": [ "# Modify the code below:\n", "\n", "l = [1,2,3,4]\n", "\n", "for element in l:\n", - " print(\"The current element in the loop is\" + element)" + " print(\"The current element in the loop is \" + str(element))" ] }, { @@ -158,14 +182,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.2188758248682006\n" + ] + }, + { + "ename": "ValueError", + "evalue": "0 multiplied by any number is always going to be zero. Please try again", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Arrulo dos Anjos\\OneDrive\\Ambiente de Trabalho\\Ironhack\\LABS\\lab-error-handling\\your-code\\main.ipynb Cell 11\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[39mreturn\u001b[39;00m math\u001b[39m.\u001b[39mlog(x\u001b[39m*\u001b[39mx)\n\u001b[0;32m 19\u001b[0m \u001b[39mprint\u001b[39m(log_square(\u001b[39m5\u001b[39m))\n\u001b[1;32m---> 20\u001b[0m \u001b[39mprint\u001b[39m(log_square(\u001b[39m0\u001b[39;49m))\n", + "\u001b[1;32mc:\\Users\\Arrulo dos Anjos\\OneDrive\\Ambiente de Trabalho\\Ironhack\\LABS\\lab-error-handling\\your-code\\main.ipynb Cell 11\u001b[0m in \u001b[0;36mlog_square\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[39m# Your code here:\u001b[39;00m\n\u001b[0;32m 15\u001b[0m \u001b[39mif\u001b[39;00m x \u001b[39m==\u001b[39m \u001b[39m0\u001b[39m:\n\u001b[1;32m---> 16\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39m0 multiplied by any number is always going to be zero. Please try again\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 17\u001b[0m \u001b[39mreturn\u001b[39;00m math\u001b[39m.\u001b[39mlog(x\u001b[39m*\u001b[39mx)\n", + "\u001b[1;31mValueError\u001b[0m: 0 multiplied by any number is always going to be zero. Please try again" + ] + } + ], "source": [ "def log_square(x):\n", " \"\"\"\n", - " This function takes a numeric value and returns the \n", - " natural log of the square of the number.\n", + " This function takes a numeric value and returns the natural log of the square of the number.\n", " The function raises an error if the number is equal to zero.\n", " Use the math.log function in this funtion.\n", " \n", @@ -176,19 +219,44 @@ " Sample Output: 3.21887\n", " \"\"\"\n", " \n", - " # Your code here:" + " # Your code here:\n", + " if x == 0:\n", + " raise ValueError(\"0 multiplied by any number is always going to be zero. Please try again\")\n", + " return math.log(x*x)\n", + "\n", + "print(log_square(5))\n", + "print(log_square(0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hi mom!\n" + ] + }, + { + "ename": "Exception", + "evalue": "The text you inserted does not contain any capital letters. Please try again", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mException\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Arrulo dos Anjos\\OneDrive\\Ambiente de Trabalho\\Ironhack\\LABS\\lab-error-handling\\your-code\\main.ipynb Cell 12\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mException\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe text you inserted does not contain any capital letters. Please try again\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 19\u001b[0m \u001b[39mprint\u001b[39m(check_capital(\u001b[39m\"\u001b[39m\u001b[39mHi mom!\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[1;32m---> 20\u001b[0m \u001b[39mprint\u001b[39m(check_capital(\u001b[39m\"\u001b[39;49m\u001b[39mwtf dad\u001b[39;49m\u001b[39m\"\u001b[39;49m))\n", + "\u001b[1;32mc:\\Users\\Arrulo dos Anjos\\OneDrive\\Ambiente de Trabalho\\Ironhack\\LABS\\lab-error-handling\\your-code\\main.ipynb Cell 12\u001b[0m in \u001b[0;36mcheck_capital\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mstr\u001b[39m(x)\n\u001b[0;32m 16\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m---> 17\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mException\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mThe text you inserted does not contain any capital letters. Please try again\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[1;31mException\u001b[0m: The text you inserted does not contain any capital letters. Please try again" + ] + } + ], "source": [ "def check_capital(x):\n", " \"\"\"\n", - " This function returns true if the string contains \n", - " at least one capital letter and throws an error otherwise.\n", + " This function returns true if the string contains at least one capital letter and throws an error otherwise.\n", " \n", " Input: String\n", " Output: Bool or error message\n", @@ -197,13 +265,21 @@ " Sample Output: True\n", " \"\"\"\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for letter in x:\n", + " if letter.isupper():\n", + " return str(x)\n", + " else:\n", + " raise Exception(\"The text you inserted does not contain any capital letters. Please try again\")\n", + "\n", + "print(check_capital(\"Hi mom!\"))\n", + "print(check_capital(\"wtf dad\"))" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3.9.12 ('base')", "language": "python", "name": "python3" }, @@ -217,7 +293,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.12" + }, + "vscode": { + "interpreter": { + "hash": "b2a2b8698d3474f38e9a1ba9d54ec85347e811d97918b8fc315ec0c82e60adc7" + } } }, "nbformat": 4,