python递归立即返回

时间:2020-04-26 09:40:27

标签: python python-3.x recursion return greatest-common-divisor

我是非常新的python编程,虽然我在if语句中编写以下代码,但确定了a的值,但是在返回调用方之后,返回值始终为none,是否缺少任何内容来将a内的值返回如果阻止递归调用。

#! /usr/bin/env python3

def gcd_calc(a, b):
    if(b == 0):
        print(a)
        return a
    c= a%b
    gcd_calc(b,c)



if __name__ == "__main__":
    a, b = map(int, input().split())

    if(a<b):
        print(gcd_calc(a,b))
    else:
        print(gcd_calc(b,a))

1 个答案:

答案 0 :(得分:1)

您缺少return

def gcd_calc(a, b):
    if(b == 0):
        print(a)
        return a
    c= a%b
    return(gcd_calc(b,c)) #you need to return here as you are calling recursively



if __name__ == "__main__":
    a, b = map(int, input().split())

    if(a<b):
        print(gcd_calc(a,b))
    else:
        print(gcd_calc(b,a))