python位置参数如何使用

Python (176) 2023-05-07 03:39:14

说明

1、实参必须按照正确的顺序传输到函数中。

2、调用函数中指定的实际参数的数量必须与形式参数的数量一致,否则Python解释器会抛出TypeError异常,并提示缺乏必要的位置参数。

实例

defgirth(width,height):
return2*width+height
#调用函数时,必须传递2个参数,否则会引发错误
print(girth(3))
运行后报Traceback错误。
Traceback(mostrecentcalllast):
File"/Volumes/Develop/Python_learn/PycharmProjects/python_demo_1/demo/function/locate_fun.py",line6,in<module>
print(girth(3))
TypeError:girth()missing1requiredpositionalargument:'height'

以上就是python位置参数的使用,希望对大家有所帮助。

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

THE END

发表回复