From ae4143ce0f3534d3c9c1f543826629bde1bd158b Mon Sep 17 00:00:00 2001 From: Davids Date: Sun, 31 Jan 2021 21:07:01 +0000 Subject: [PATCH] [lab-error-handling] - David Morazzo --- ...andling] - David Morazzo-checkpoint.ipynb} | 208 ++++++++------ ...[lab-error-handling] - David Morazzo.ipynb | 263 ++++++++++++++++++ 2 files changed, 386 insertions(+), 85 deletions(-) rename your-code/{main.ipynb => .ipynb_checkpoints/[lab-error-handling] - David Morazzo-checkpoint.ipynb} (58%) create mode 100644 your-code/[lab-error-handling] - David Morazzo.ipynb diff --git a/your-code/main.ipynb b/your-code/.ipynb_checkpoints/[lab-error-handling] - David Morazzo-checkpoint.ipynb similarity index 58% rename from your-code/main.ipynb rename to your-code/.ipynb_checkpoints/[lab-error-handling] - David Morazzo-checkpoint.ipynb index 2a08c31..b112134 100644 --- a/your-code/main.ipynb +++ b/your-code/.ipynb_checkpoints/[lab-error-handling] - David Morazzo-checkpoint.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,82 +33,86 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", - " \n", - " Input: Real number\n", - " Output: Real number\n", - " \n", - " Sample Input: -4\n", - " Sample Output: 2.0\n", - " \"\"\"\n", - " \n", + " if x < 0:\n", + " x = abs(x)\n", " return math.sqrt(x)\n", "\n", - "sqrt_for_all(-1)" + "sqrt_for_all(-1)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "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 y == 0:\n", + " return 0\n", + " else:\n", + " return x / y\n", "\n", - "divide(5, 0)" + "divide(5, 0)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[11]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "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 not isinstance(l, list):\n", + " l = [l]\n", + " return [a + element for element in l];\n", " \n", - "add_elements(5, 6)" + "add_elements(5, 6)\n" ] }, { @@ -122,29 +126,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "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 + 1 for element in l])\n" ] }, { "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))\n" ] }, { @@ -158,47 +184,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Log Square of 5: 3.2188758248682006\n", + "Log Square of 2: 1.3862943611198906\n" + ] + } + ], "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:" + " if x == 0:\n", + " raise ValueError(\"The input cannot be zero\")\n", + " return math.log(x ** 2)\n", + "\n", + "print(f\"Log Square of 5: {log_square(5)}\")\n", + "print(f\"Log Square of 2: {log_square(2)}\")\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ] + } + ], "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:" + " if any(y.isupper() for y in x):\n", + " return True\n", + " else:\n", + " raise ValueError(\"The string does not contain at least one upper case letter\")\n", + " \n", + "print(check_capital('David'))\n", + "print(check_capital('Morazzo'))\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -217,7 +255,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.3" } }, "nbformat": 4, diff --git a/your-code/[lab-error-handling] - David Morazzo.ipynb b/your-code/[lab-error-handling] - David Morazzo.ipynb new file mode 100644 index 0000000..b112134 --- /dev/null +++ b/your-code/[lab-error-handling] - David Morazzo.ipynb @@ -0,0 +1,263 @@ +{ + "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": 1, + "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": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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 x < 0:\n", + " x = abs(x)\n", + " return math.sqrt(x)\n", + "\n", + "sqrt_for_all(-1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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 y == 0:\n", + " return 0\n", + " else:\n", + " return x / y\n", + "\n", + "divide(5, 0)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[11]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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", + " if not isinstance(l, list):\n", + " l = [l]\n", + " return [a + element for element in l];\n", + " \n", + "add_elements(5, 6)\n" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "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])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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 \" + str(element))\n" + ] + }, + { + "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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Log Square of 5: 3.2188758248682006\n", + "Log Square of 2: 1.3862943611198906\n" + ] + } + ], + "source": [ + "def log_square(x):\n", + " if x == 0:\n", + " raise ValueError(\"The input cannot be zero\")\n", + " return math.log(x ** 2)\n", + "\n", + "print(f\"Log Square of 5: {log_square(5)}\")\n", + "print(f\"Log Square of 2: {log_square(2)}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ] + } + ], + "source": [ + "def check_capital(x):\n", + " if any(y.isupper() for y in x):\n", + " return True\n", + " else:\n", + " raise ValueError(\"The string does not contain at least one upper case letter\")\n", + " \n", + "print(check_capital('David'))\n", + "print(check_capital('Morazzo'))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}