使用良好的函数命名
好的函数名可以大幅提升用户和维护者的理解效率。好的函数名应当清晰地描述函数的作用。在本练习中,您将为一个函数选择合适的名称,以提高它在使用时的可读性。
本练习是课程的一部分
Python 中的软件工程原理
练习说明
- 环境中已预先加载
math模块,您可以使用其中的sqrt函数。 - 请从以下选项中为函数选择最合适的名称:
do_stuff、hypotenuse_length、square_root_of_leg_a_squared_plus_leg_b_squared、pythagorean_theorem。 - 使用您选择的函数名,完成文档字符串中的示例。
- 使用新命名的函数,求直角三角形两条直角边长度为
6和8时的斜边长度,并将结果用print打印出来。
交互式实操练习
通过完成这段示例代码来试试这个练习。
def ____(leg_a, leg_b):
"""Find the length of a right triangle's hypotenuse
:param leg_a: length of one leg of triangle
:param leg_b: length of other leg of triangle
:return: length of hypotenuse
>>> ____(3, 4)
5
"""
return math.sqrt(leg_a**2 + leg_b**2)
# Print the length of the hypotenuse with legs 6 & 8
print(____)