简体   繁体   English

我需要帮助,用Python及其在此递归语句中的作用来绕过return语句

[英]I need help wrapping my head around the return statement with Python and its role in this recursive statement

No this isn't homework but it is on our study guide for a test. 不,这不是功课,而是在我们的学习指南中进行测试。 I need to understand the role the return statement plays and the role recursion plays. 我需要了解return语句和递归的作用。 I don't understand why the function doesn't break after x = 1. 我不明白为什么x = 1后函数不会中断。

def thisFunc(x):
    print(x)
    if x>1:
         result=thisFunc(x-1)
         print(result)
    return x+1

Sorry, I understand how elementary this is but I could really use some help. 抱歉,我知道这很基础,但我确实可以使用一些帮助。 Probably why I can't find an explanation anywhere...because it's so simple. 可能是为什么我在任何地方都找不到解释...因为它是如此简单。

edit: Why does it print out what it does and what and why is the value of x at the end? 编辑:为什么它会打印出它的功能,以及为什么以及最后x的值是什么? sorry if I'm asking a lot I'm just frustrated 抱歉,如果我问很多,我很沮丧

When you enter the function with a value n>1 it prints the current value, and then calls it's self with n-1 . 当您输入值n>1的函数时,它将打印当前值,然后使用n-1调用其自身。 When the inner function returns it returns the value n - 1 + 1 which is just n . 当内部函数返回时,它返回值n - 1 + 1 ,即n Hence, the function prints out the value n twice, once before the inner recursion and once after. 因此,该函数两次打印出值n ,一次在内部递归之前,一次在之后。

If n == 1 , which is the base case, the function only prints 1 once and does not call it self again (and hence does not get result back to print). 如果n == 1 (这是基本情况),则该函数仅打印1次,并且不会再次对其自身进行调用(因此不会将result返回打印)。 Instead it just returns, hence why 1 is only printed once. 相反,它只是返回,因此为什么1只打印一次。

Think of it like an onion. 认为它像洋葱。

calling thisFunc(n) will result in 调用thisFunc(n)将导致

n
# what ever the output (via print) of thisFunc(n-1) is
n 

I don't understand why the function doesn't break after x = 1. 我不明白为什么x = 1后函数不会中断。

But it does: 但这确实是:

>>> ================================ RESTART ================================
>>> x = 1
>>> def thisFunc(x):
    print("Function called on x-value: ", x)
    if x > 1:
        result = thisFunc(x-1)
        print(result)
    return x+1

>>> thisFunc(x)
Function called on x-value:  1
2
>>>

edit: Why does it print out what it does and what and why is the value of x at the end? 编辑:为什么它会打印出它的功能,以及为什么以及最后x的值是什么? Well, it prints it out because you're telling it to. 好吧,它会打印出来,因为您是在告诉它。 Try following the value of x as you go through the function ("x is one, one is not bigger than 1; return 1+1. Ok. [new case] x is two, two is bigger than 1..." and so on). 试试下面的x的值,当您浏览的功能(“x是一个,一个是不大于 1;返回1 + 1 OK [新情况X是二,二生大于1 ...”和依此类推)。

return and recursion are part and parcel of programming; returnrecursion是编程的一部分; return statements designates the end of a function (even if you might have several lines more of code) and they also pass data back to whatever asked them for it. return语句指定函数的结尾(即使您可能还有几行代码),它们也将数据传递回要求它们的任何对象。 In your case you're asking "what happens when x is 1, given these rules?"; 在您的情况下,您要问“给定这些规则,当x为1时会发生什么?” the returned data is your answer. 返回的数据就是您的答案。

Recursion is simply the matter of letting the function call itself, should it (you) need to. 递归只是(如果需要)让函数调用自身的问题。 You simply tell the program that "hey, as long as x is bigger than 1, call this function [that just so happens to be the same function initially called] on it and let it do its thing". 您只需告诉程序“嘿,只要x大于1,就可以在其上调用此函数[恰好恰好是最初调用的相同函数]”。 To get a better understanding of your function I'd suggest that you add the line "Function called on x-value: " to the first print statement inside the function, or at least something that lets you identify which printed line is x and which is result . 为了更好地了解您的函数,建议您在"Function called on x-value: "内部的第一条print语句中添加"Function called on x-value: "行,或者至少添加一些行,以使您识别出哪个打印行是x以及哪个行是result

For a more in-depth explanation on recursion, I recommend Recursion explained with the flood fill algorithm and zombies and cats 有关递归的更深入说明,我建议使用泛洪填充算法以及僵尸和猫来解释递归。

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

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