<jupyter_start><jupyter_text>Before your start:- Read the README.md file- Comment as much as you can and use the resources in the README.md file- Happy learning!<jupyter_code># Libraries
import math<jupyter_output><empty_output><jupyter_text>Challenge 1 - Handling Errors Using `if` StatementsIn 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.In the 3 cells below, add an `if` statment that will handle both types of input allowed in the functions.<jupyter_code># Modify the code below to handle positive and negative numbers by adding an if statement and performing a transformation:

import math
# checking if math imported correctly
# math.pi

def sqrt_for_all(x):
    """
    This function will take any real number and 
    return the square root of its magnitude.
    
    Input: Real number
    Output: Real number
    
    Sample Input: -4
    Sample Output: 2.0
    """
    
    if x >= 0:
        return math.sqrt(x)
    
    elif x < 0:
        return math.sqrt(-x)
    
    else: 
        print("Value entered is not a number")
    

sqrt_for_all(9)
# Modify the code below to handle zero as well. In the case of zero, return zero

def divide(x, y):
    """
    This function will take any two real numbers 
    and return their quotient. 
    If the denominator is zero, we return zero.
    
    Input: Real number
    Output: Real number
    
    Sample Input: 5, 1
    Sample Output: 5.0
    """
    if y != 0: 
        return x / y
    else: 
        raise ValueError("division by zero")

try:
    result = divide(5, 5)
    print(result)
except: 
    print("0")

# Modify the function below that it will take either an number and a list or two numbers. 
# If we take two numbers, add them together and return a list of length 1. 
# Otherwise, add the number to every element of the list and return the resulting list

def add_elements(a, l):
    """
    This function takes either two numbers or a list and a number 
    and adds the number to all elements of the list.
    If the function only takes two numbers, it returns a list 
    of length one that is the sum of the numbers.
    
    Input: number and list or two numbers
    Output: list
    
    Sample Input: 5, 6
    Sample Output: [11]
    """
    if type(a) and type(l) == int: 
        total = [a+l]
        return total 
    elif type(a) or type(l) == int and type(a) or type(l) == list:    
        for ind,val in enumerate(l):
            l[ind] = l[ind]+a
            total = l
        return total 
    else: 
        print("Please enter a number and a list or two numbers")
#     return [a + element for element in l]
        
print(add_elements(5, [6,1]))<jupyter_output>[11, 6]<jupyter_text>Challenge 2 - Fixing Errors to Get Code to RunSometimes 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.<jupyter_code># Modify the code below:

l = [1,2,3,4]

sum([element + 1 for element in l])
# Modify the code below:

l = [1,2,3,4]

for element in l:
    print("The current element in the loop is " + str(element))<jupyter_output>The current element in the loop is 1
The current element in the loop is 2
The current element in the loop is 3
The current element in the loop is 4<jupyter_text>Bonus Challenge - Raise Errors on Your OwnThere 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.<jupyter_code>def log_square(x):
    """
    This function takes a numeric value and returns the 
    natural log of the square of the number.
    The function raises an error if the number is equal to zero.
    Use the math.log function in this funtion.
    
    Input: Real number
    Output: Real number or error
    
    Sample Input: 5
    Sample Output: 3.21887
    """
    
    # Your code here:
x=10

if x==0:
    raise ValueError ("Number cannot equal to zero")
else: 
    print(math.log(x**2))



def check_capital(x):
    """
    This function returns true if the string contains 
    at least one capital letter and throws an error otherwise.
    
    Input: String
    Output: Bool or error message
    
    Sample Input: 'John'
    Sample Output: True
    """
    x = 'John'
    # Your code here:
import re   

x = 'joHn'
if len(re.findall('[A-Z]', x))>=1: 
    print('True')
else: 
    raise NameError ("String must have at least one capital letter")<jupyter_output>True
{'fraction_non_alphanumeric': 0.06813142511590405, 'fraction_numerical': 0.011086474501108648, 'mean_word_length': 2.858475894245723, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '145122', 'license': 'MIT', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1412, 'n_words': 712, 'path': 'module-1/Error-Handling/your-code/main.ipynb', 'repo_name': 'ruesaintdenis/daft-miami-0120-labs', 'size': '4961'}