简体   繁体   English

为什么在python中使用递归时会发生这种情况?

[英]Why does this happen when using recursion in python?

I am learning resursion recently and I wrote a simple recursive function to verify my understanding:最近在学习resursion,写了一个简单的递归函数来验证我的理解:

def hello(n):
    if n == 1:
        return 'hello'
    else:
        print('hello')
        hello(n-1)

def returnhello():
    return 'hello'

print(returnhello())
print()
print(hello(5))

the output of this is shown here:此处显示了此输出:

hello

hello
hello
hello
hello
None

Why the last call in the recursion prints None instead of hello?为什么递归中的最后一个调用打印 None 而不是 hello? I was expecting it to print 5 hello我期待它打印 5 hello

This is because in your else part in hello(n) you don't have a return statement before hello(n-1) , so the first call (exiting the last) will return a None .这是因为在hello(n) else部分中,您在hello(n-1)之前没有return语句,因此第一个调用(退出最后一个调用)将返回None

If you put a return before hello(n-1) you should get what you want.如果你在hello(n-1)之前放一个 return ,你应该得到你想要的。

The right recursion function for your expected output is:预期输出的正确递归函数是:

def hello(n):
    if n == 1:
        return 'hello'
    else:
        print('hello')
        return hello(n-1)

def returnhello():
    return 'hello'

print(returnhello())
print()
print(hello(5))

Also, it can be written as:另外,它可以写成:

def hello(n):
    if n==1:
        print("hello")
    else:
        print("hello")
        hello(n-1)
        
def returnhello():
    return 'hello'

print(returnhello())
print()
hello(5)

The output will be:输出将是:

hello

hello
hello
hello
hello
hello

Note:笔记:

You mustn't use print with a recursive function, you can use a function with return statement or without any statement.您不能将print与递归函数一起使用,您可以使用带有 return 语句或不带任何语句的函数。

@saedx has identified and corrected your issue. @saedx 已确定并更正了您的问题。 Python returns None by default and that is what you see printed after the function returns. Python 默认返回None ,这就是您在函数返回后看到的打印内容。

You could implement your hello function to be a bit more consistent in how it displays the strings.您可以实现hello函数,使其显示字符串的方式更加一致。 At present the first n-1 are printed in the body of the function but the caller is left to print the final one.目前,第一个 n-1 被打印在函数体中,但调用者要打印最后一个。

Here the function prints all n of the strings.这里函数打印所有 n 个字符串。

def hello(n):
    print('hello')
    if n > 1:
        hello(n-1)

hello(5)

In this case you just call the function.在这种情况下,您只需调用该函数。 You don't print what it returns.你不打印它返回的内容。

The other way would be to have the caller print all n of the strings.另一种方法是让调用者打印所有 n 个字符串。

def hello(n):
    yield 'hello'
    if n > 1:
        yield from hello(n-1)

Which is then called like然后被称为

print('\n'.join(hello(5)))

Also note that both these examples remove the duplication of the string being printed.另请注意,这两个示例都删除了正在打印的字符串的重复项。 Its worth noting that if you passed in a number less than 1 you'd be in trouble, as it would recurse infinitely.值得注意的是,如果你传入一个小于 1 的数字,你会遇到麻烦,因为它会无限递归。 So we could throw an exception in that case.所以在这种情况下我们可以抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM