综合运用

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...