9 月 232020
 
#位置参数和关键词参数
#自定义函数,求梯形面积,上底,下底,高。
def trapezoid_area(base_up, base_down, height):
    return 1/2 * (base_up + base_down) * height

#先给变量赋值,再调用函数
base_up = 1
base_down = 2
height = 3

#比较
print(trapezoid_area(1, 2, 3))

#函数中的参数在传入之前已经有了具体值,因此这是位置参数传入而不是关键词参数传入
print(trapezoid_area(height, base_down, base_up))
#print(trapezoid_area(3, 2, 1))
# 1/2 * (3 + 2) * 1 即 2.5,而不是4.5 
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
4.5
2.5
PS C:\Users\harveymei> 

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)