【问题标题】:Recursive function that return result from 'if' statement instead of 'else' statement从“if”语句而不是“else”语句返回结果的递归函数
【发布时间】:2015-08-03 12:22:50
【问题描述】:

我是 Python 的新手,我正在尝试编写一个简单的递归函数:

def bugged_recursion(inp_value,list_index=0):
    '''Define a recursive function that tag lists according to one parameter.
    '''
    #check if a criterion is true at position 0 in the list
    if list_index == 0:
        if inp_value[list_index] == 'valid':
            status = 'valid inp_value'
        #if the criterion is false call the function at the next index
        else:
            status = 'invalid inp'
            list_index +=1
            bugged_recursion(inp_value,list_index=list_index)
    #check if a criterion is true at position 1 in the list
    else:
        if inp_value[list_index] == 'valid':
            status = 'index {} is a valid inp_value'.format(list_index)
        else:
            status = 'index is never a valid inp_value'
    print(status)
    #return the input and its status
    return (inp_value,status)

if __name__ == '__main__':
    inp_value = ['invalid','invalid']
    bugged_recursion(inp_value)

我不明白为什么这个函数会返回 if 语句中的状态,而不是返回最后一个 else 语句中包含的状态。 对我来说,最奇怪的是它会在某些时候打印正确的状态,但不会返回。

我无法理解为什么...我真的很好奇如何使用递归函数来执行此任务。

【问题讨论】:

    标签: python-3.x recursion


    【解决方案1】:

    哇哇,这是多么的折磨。

    def bugged_recursion(inp_value, list_index=0):
        # i don't get why you compare list_index here
        if list_index == 0:
            # you'll get an IndexError if list_index > len(inp_value)
            if inp_value[list_index] == 'valid':
                status = 'valid inp_value'
            else:
                status = 'invalid inp'
                # there is only one place where you increment list_index
                # i suppose there is something wrong here
                list_index +=1
                # you forgot to return here
                return bugged_recursion(inp_value, list_index=list_index)
        else:
            if inp_value[list_index] == 'valid':
                status = 'index {} is a valid inp_value'.format(list_index)
            else:
                status = 'index is never a valid inp_value'
        return (inp_value,status)
    

    除此之外,人们通常倾向于尽可能避免recursion(例如Dive into Python)。

    这是否满足您的需求?

    def no_recursion(inp_value):
        for i, val in enumerate(inp_value):
            # you probably have a good reason to test the index
            if i == 0:
                if val == 'valid':
                    yield 'valid inp_value: %s' % val
                else:
                    yield 'invalid inp: %s' % val
            else:
                yield 'index %s is %s valid inp_value' % (
                    i,
                    'a' if val == 'valid' else 'never'
                )
    
    print tuple(no_recursion(inp_value))
    

    给:('invalid inp: invalid', 'index 1 is never valid inp_value')

    【讨论】:

    • 感谢您解释如何更正我的代码以及如何编写更好的非递归函数。我不知道有必要返回 bugged_recursion() 抱歉,如果我看起来很天真,但我是一个自学者,我会看看你的链接!感谢您的帮助,我很高兴今天学到了一些东西!