<tfoot id='h94K9'></tfoot>

    <small id='h94K9'></small><noframes id='h94K9'>

        <bdo id='h94K9'></bdo><ul id='h94K9'></ul>

    1. <legend id='h94K9'><style id='h94K9'><dir id='h94K9'><q id='h94K9'></q></dir></style></legend>

    2. <i id='h94K9'><tr id='h94K9'><dt id='h94K9'><q id='h94K9'><span id='h94K9'><b id='h94K9'><form id='h94K9'><ins id='h94K9'></ins><ul id='h94K9'></ul><sub id='h94K9'></sub></form><legend id='h94K9'></legend><bdo id='h94K9'><pre id='h94K9'><center id='h94K9'></center></pre></bdo></b><th id='h94K9'></th></span></q></dt></tr></i><div id='h94K9'><tfoot id='h94K9'></tfoot><dl id='h94K9'><fieldset id='h94K9'></fieldset></dl></div>

    3. 我需要帮助来理解使用 Python 的 return 语句及其在此递归语句中的作用

      I need help wrapping my head around the return statement with Python and its role in this recursive statement(我需要帮助来理解使用 Python 的 return 语句及其在此递归语句中的作用)
          <i id='kkaN9'><tr id='kkaN9'><dt id='kkaN9'><q id='kkaN9'><span id='kkaN9'><b id='kkaN9'><form id='kkaN9'><ins id='kkaN9'></ins><ul id='kkaN9'></ul><sub id='kkaN9'></sub></form><legend id='kkaN9'></legend><bdo id='kkaN9'><pre id='kkaN9'><center id='kkaN9'></center></pre></bdo></b><th id='kkaN9'></th></span></q></dt></tr></i><div id='kkaN9'><tfoot id='kkaN9'></tfoot><dl id='kkaN9'><fieldset id='kkaN9'></fieldset></dl></div>

              <tfoot id='kkaN9'></tfoot>

                <tbody id='kkaN9'></tbody>

                <bdo id='kkaN9'></bdo><ul id='kkaN9'></ul>
              • <small id='kkaN9'></small><noframes id='kkaN9'>

                <legend id='kkaN9'><style id='kkaN9'><dir id='kkaN9'><q id='kkaN9'></q></dir></style></legend>
              • 本文介绍了我需要帮助来理解使用 Python 的 return 语句及其在此递归语句中的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                不,这不是家庭作业,而是在我们的考试学习指南中.我需要了解 return 语句所扮演的角色以及递归所扮演的角色.我不明白为什么函数在 x = 1 之后不会中断.

                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. I don't understand why the function doesn't break after 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:为什么它会打印出它的作用以及最后的 x 值是什么以及为什么?对不起,如果我问了很多,我只是很沮丧

                edit: Why does it print out what it does and what and why is the value of x at the end? sorry if I'm asking a lot I'm just frustrated

                推荐答案

                当你输入带有值 n>1 的函数时,它会打印当前值,然后用 调用它自己n-1.当内部函数返回时,它返回值 n - 1 + 1,即 n.因此,该函数两次打印出 n 值,一次在内部递归之前,一次在之后.

                When you enter the function with a value n>1 it prints the current value, and then calls it's self with n-1. When the inner function returns it returns the value n - 1 + 1 which is just n. Hence, the function prints out the value n twice, once before the inner recursion and once after.

                如果 n == 1,这是基本情况,该函数只打印一次 1 并且不会再次调用它自己(因此不会得到 结果返回打印).相反,它只是返回,因此 1 只打印一次.

                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). Instead it just returns, hence why 1 is only printed once.

                把它想象成洋葱.

                调用 thisFunc(n) 将导致

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

                这篇关于我需要帮助来理解使用 Python 的 return 语句及其在此递归语句中的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                <i id='oMnpT'><tr id='oMnpT'><dt id='oMnpT'><q id='oMnpT'><span id='oMnpT'><b id='oMnpT'><form id='oMnpT'><ins id='oMnpT'></ins><ul id='oMnpT'></ul><sub id='oMnpT'></sub></form><legend id='oMnpT'></legend><bdo id='oMnpT'><pre id='oMnpT'><center id='oMnpT'></center></pre></bdo></b><th id='oMnpT'></th></span></q></dt></tr></i><div id='oMnpT'><tfoot id='oMnpT'></tfoot><dl id='oMnpT'><fieldset id='oMnpT'></fieldset></dl></div>

                  • <legend id='oMnpT'><style id='oMnpT'><dir id='oMnpT'><q id='oMnpT'></q></dir></style></legend>

                      <small id='oMnpT'></small><noframes id='oMnpT'>

                        <bdo id='oMnpT'></bdo><ul id='oMnpT'></ul>
                          <tbody id='oMnpT'></tbody>
                        <tfoot id='oMnpT'></tfoot>