Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我认为我没有正确调用/返回程序中的函数_Python 3.x_Return - Fatal编程技术网

Python 3.x 我认为我没有正确调用/返回程序中的函数

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

我不能让它正确运行,我必须使用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 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
    无法访问它们,因为当前正在编写代码。最后,我认为周长计算中有一个错误。