Bringing it all together

Introduction to Functions in Python

Hugo Bowne-Anderson

Instructor

Errors and exceptions

def sqrt(x):
    try:
        return x ** 0.5
    except:
        print('x must be an int or float')
sqrt(4)
2.0
sqrt('hi')
x must be an int or float
Introduction to Functions in Python

Errors and exceptions

def sqrt(x):
    if x < 0:
        raise ValueError('x must be non-negative')
    try:
        return x ** 0.5
    except TypeError:
        print('x must be an int or float')
Introduction to Functions in Python

Let's practice!

Introduction to Functions in Python

Preparing Video For Download...