สรุปทุกอย่างเข้าด้วยกัน

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 เบื้องต้น: การเขียนฟังก์ชัน

มาฝึกกันเถอะ!

Python เบื้องต้น: การเขียนฟังก์ชัน

Preparing Video For Download...