返回函数或值:递归python函数

2024-09-28 01:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

有人愿意解释为什么第一种方法不起作用,而第二种方法起作用吗

首先,函数计算最终的调整值

# returns None
def _pareRotation(degs):
    if degs > 360:          
        _pareRotation(degs - 360)
    else:
        print "returning %s" % degs
        return degs

…但返回None

print _pareRotation(540)
>> returning 180
>> None

但是,如果我们稍微翻转一下并返回函数

# returns expected results
def _pareRotation(degs):
    if degs < 360:          
        print "returning %s" % degs     
        return degs
    else:
        return _pareRotation(degs - 360)

…它工作正常:

print _pareRotation(540)
>> returning 180
>> 180

主要是想知道是什么导致None从递归循环中弹出


Tags: 方法函数nonereturnifdefelseresults

热门问题