綜合應用

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