Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions your-code/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
330 changes: 330 additions & 0 deletions your-code/.ipynb_checkpoints/main-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
{
"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": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 4,
"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",
"# to handle negative number we have to create an if statement that will check if input negative\n",
"# If so, it will add a \"-\" as negtive * negative = positive\n",
" if x < 0:\n",
" x = -x\n",
" return math.sqrt(x)\n",
"\n",
"sqrt_for_all(-4)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 25,
"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",
" if y ==0:\n",
" return 0\n",
" \n",
" return x / y\n",
"\n",
"divide(5, 0)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 7, 8, 9]"
]
},
"execution_count": 29,
"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",
" lst =[]\n",
" if type(l) == int:\n",
" lst.append(l)\n",
" return [a + element for element in lst]\n",
" \n",
" return [a + element for element in l]\n",
" \n",
"add_elements(5, [1,2,3,4])"
]
},
{
"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": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 35,
"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])"
]
},
{
"cell_type": "code",
"execution_count": 39,
"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\",+ 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": 51,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Please input a numeric value different than 0",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/_0/46b7d7lx5gxfy45sbjf5py3h0000gn/T/ipykernel_1643/2441468105.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mlog_square\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/var/folders/_0/46b7d7lx5gxfy45sbjf5py3h0000gn/T/ipykernel_1643/2441468105.py\u001b[0m in \u001b[0;36mlog_square\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# Your code here:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Please input a numeric value different than 0'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: Please input a numeric value different than 0"
]
}
],
"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:\n",
" \n",
" raise ValueError('Please input a numeric value different than 0')\n",
" \n",
" return math.log(x**2)\n",
"\n",
"log_square(0)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"ename": "Exception",
"evalue": "No capital letter",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/_0/46b7d7lx5gxfy45sbjf5py3h0000gn/T/ipykernel_1643/2681557953.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[A-Z]\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mcheck_capital\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'john'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/var/folders/_0/46b7d7lx5gxfy45sbjf5py3h0000gn/T/ipykernel_1643/2681557953.py\u001b[0m in \u001b[0;36mcheck_capital\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[A-Z]\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'No capital letter'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[A-Z]\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mException\u001b[0m: No capital letter"
]
}
],
"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:\n",
" import re\n",
" \n",
" if bool(re.match(\"[A-Z]\", x)) == False:\n",
" raise Exception('No capital letter')\n",
" \n",
" return bool(re.match(\"[A-Z]\", x))\n",
"\n",
"check_capital('john')\n",
" "
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 6 additions & 0 deletions your-code/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
Loading