Python 3.x 我认为我没有正确调用/返回程序中的函数
我不能让它正确运行,我必须使用3个函数,它们的用途是我设置它们的目的Python 3.x 我认为我没有正确调用/返回程序中的函数,python-3.x,return,Python 3.x,Return,我不能让它正确运行,我必须使用3个函数,它们的用途是我设置它们的目的 def lw(): l = input("Enter the length of your rectangle: ") w = input("Now enter the width of your rectangle:") return l, w def ap(): l,w = lw() area = l * w perimeter = 2*1 + 2*w return
def lw():
l = input("Enter the length of your rectangle: ")
w = input("Now enter the width of your rectangle:")
return l, w
def ap():
l,w = lw()
area = l * w
perimeter = 2*1 + 2*w
return area, perimeter
def main():
area,perimeter = ap()
print("With a length of", l ."and a width of", w)
print("the area of your rectangle is", area)
print("the perimeter of your rectangle is", perimeter)
if __name__ == "__main__":
main()
这应该行得通
def lw():
l = input("Enter the length of your rectangle: ")
w = input("Now enter the width of your rectangle:")
return l, w
def ap():
l,w = lw()
area = l * w
perimeter = 2*1 + 2*w
return l, w, area, perimeter
def main():
l,w,area,perimeter = ap()
print("With a length of", l ,"and a width of", w)
print("the area of your rectangle is", area)
print("the perimeter of your rectangle is", perimeter)
if __name__ == "__main__":
main()
我做了两个更改:在
ap()
函数中传递l
和w
,并在main()
函数中访问它们
我可以看到您的代码存在许多问题:
main
函数中的第一条print
语句似乎在调用该函数范围内不存在的变量。除了区域
和周长
之外,还需要从ap()
返回l
和w
的值。这个语句的参数也有一点输入错误(a
其中a,
应该是)2
乘以1
而不是2
乘以l
,从而使l
无效字符串
值,而不是数字值。如果要计算任何内容,需要将这些函数传递到int()
或float()
中,并返回这些函数的结果l
和w
都是字符串,不是整数或浮点数或任何您需要的数字。您需要将输入(…)
调用包装在int()
或float
调用中。此外,l
和w
是lw
和ap
函数中的局部变量main
无法访问它们,因为当前正在编写代码。最后,我认为周长计算中有一个错误。