すべてをまとめる

Pythonの関数入門

Hugo Bowne-Anderson

Instructor

エラーと例外

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
Pythonの関数入門

エラーと例外

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')
Pythonの関数入門

Let's practice!

Pythonの関数入門

Preparing Video For Download...