Python 函数

2017-12-08  本文已影响0人  E_H_I_P

参数检查

def my_abs(x):
    if not isinstance(x,(int,float)):
        raise TypeError('Bad operand type')
    if x >= 0:
        return x
    else:
        return -x
a = my_abs(-10)
print(a)

默认参数

def power_n(x,n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

a = power_n(5,3)
print(a)
上一篇 下一篇

猜你喜欢

热点阅读