Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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 再一次';返回';外部功能_Python_Return - Fatal编程技术网

Python 再一次';返回';外部功能

Python 再一次';返回';外部功能,python,return,Python,Return,我试图编写一个模块来计算最小公倍数,但我总是得到相同的错误:函数外部的“return” def lcm(x,y): amin = min(x,y) for j in range(1, amin + 1) : if(x%j==0 and y%j==0) : jmax = j z= (x*y)/(jmax) return z 我尝试缩进返回行,但实际上它与缩进在同一级别,如果,我不知道该怎么办。函数体也需要缩进: def lc

我试图编写一个模块来计算最小公倍数,但我总是得到相同的错误:函数外部的“return”

def lcm(x,y):

amin = min(x,y)

for j in range(1, amin + 1) :

     if(x%j==0 and y%j==0) :

          jmax = j

          z= (x*y)/(jmax)

     return z

我尝试缩进
返回
行,但实际上它与
缩进在同一级别,如果
,我不知道该怎么办。

函数体也需要缩进:

def lcm(x,y):
    amin = min(x,y)

    for j in range(1, amin + 1) :
         if x%j==0 and y%j==0 :  # also, no need to use parentheses there
              jmax = j
              z= (x*y)/(jmax)

    return z

简单地说:您已经用
def
定义了一个函数,但没有在其中放入任何代码。
amin
和for循环的缩进方式与
def
相同。尝试将它们缩进4个空格,使它们成为函数的一部分

所以


您需要从
def
@Fejs缩进我还原了您的编辑,因为错误的缩进正是本问题讨论的错误。将它们缩进4个空格,如pep8指定;)我按4缩进,但说是2。我道歉
def your_function():
    amin = something
    for i in x:
        # do something
    return xyz