1.位置参数
位置参数可以在函数中设置一个或者多个参数,但是必须有对应个数的值传入该函数才能成功调用,例如:
def power(x): return x*x print(powr(5))
如果传入的值与对应函数设置的位置参数不符合,则会报错:
Traceback (most recent call last):
File "D:\Python\pythonProject\work03.py", line 107, in <module>
print(power(5))
TypeError: power() missing 1 required positional argument: 'n'def power(x,n): s = 1 while n > 0: n = n - 1 s = s * x return s print(powr(5))
正确的调用方式应为:
def power(x,n): s = 1 while n > 0: n = n - 1 s = s * x return s print(power(5,3))
传入参数的类型根据调用函数时传入的值所决定,例如: