错误提示
比如使用求绝对值函数abs(),如果传入两个或以上参数
>>> abs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abs() takes exactly one argument (2 given)
TypeError: abs() takes exactly one argument (2 given)
如果传入的数量对,类型不对
>>> abs('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
TypeError: bad operand type for abs(): ‘str’
数据类型转换
python中有意思的一个点,数据类型加()括号,就可以转换数据类型
比如:
>>> int('123')
123
>>> int(12.34)
12
>>> float('12.34')
12.34
>>> str(1.23)
'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False
函数名还可以赋值给一个变量,相对于给函数起了一个别名(函数指针吗?)
>>> a = abs
>>> a(-1)
>1
小结
调用Python的函数,需要根据函数定义,传入正确的参数。如果函数调用出错,一定要学会看错误信息,所以英文很重要!
英语很重要,英语很重要,英语很重要。