Tổng hợp lại

Giới thiệu về Functions trong Python

Hugo Bowne-Anderson

Instructor

Lỗi và ngoại lệ

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
Giới thiệu về Functions trong Python

Lỗi và ngoại lệ

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')
Giới thiệu về Functions trong Python

Hãy thực hành!

Giới thiệu về Functions trong Python

Preparing Video For Download...