【问题标题】:Why doesn't the return statement terminate the function? [duplicate]为什么 return 语句不终止函数? [复制]
【发布时间】:2020-05-21 21:44:24
【问题描述】:
def gcd_fast(a, b):
    if(b > a):
        a, b = b, a


    if(a % b == 0):
        return b
    else:
        rem = a % b
        print('C:' + str(rem))
        gcd_fast(b, rem)    
print(gcd_fast(10, 9))

它不返回任何内容,并且在返回语句之后使用调试器运行它时,它会在 if 子句中跳回到 gcd_fast(a, b)。我一点都不擅长python

【问题讨论】:

标签: python python-3.x recursion return


【解决方案1】:
def gcd_fast(a, b):
    if(b > a):
        a, b = b, a


    if(a % b == 0):
        return b
    else:
        rem = a % b
        print('C:' + str(rem))
        return gcd_fast(b, rem)    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2022-11-20
    相关资源
    最近更新 更多