• <small id='s2VzU'></small><noframes id='s2VzU'>

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

      1. <tfoot id='s2VzU'></tfoot><legend id='s2VzU'><style id='s2VzU'><dir id='s2VzU'><q id='s2VzU'></q></dir></style></legend>
      2. 如何从递归 Python 函数中返回一个值?

        How to return a value from a recursive Python function?(如何从递归 Python 函数中返回一个值?)
          1. <tfoot id='6cG57'></tfoot>

              <bdo id='6cG57'></bdo><ul id='6cG57'></ul>

                  <tbody id='6cG57'></tbody>

                  <legend id='6cG57'><style id='6cG57'><dir id='6cG57'><q id='6cG57'></q></dir></style></legend>

                  <small id='6cG57'></small><noframes id='6cG57'>

                  <i id='6cG57'><tr id='6cG57'><dt id='6cG57'><q id='6cG57'><span id='6cG57'><b id='6cG57'><form id='6cG57'><ins id='6cG57'></ins><ul id='6cG57'></ul><sub id='6cG57'></sub></form><legend id='6cG57'></legend><bdo id='6cG57'><pre id='6cG57'><center id='6cG57'></center></pre></bdo></b><th id='6cG57'></th></span></q></dt></tr></i><div id='6cG57'><tfoot id='6cG57'></tfoot><dl id='6cG57'><fieldset id='6cG57'></fieldset></dl></div>
                1. 本文介绍了如何从递归 Python 函数中返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个问题似乎有点具体,对此我很抱歉,但这让我很困惑.我正在为自己编写一个密码生成器,它接受一个字符串(也就是网站的 URL)并将其处理成一个安全密码,该密码不能根据网站名称回溯.

                  This question seems a bit specific, and for that I'm sorry, but it has me stumped. I'm writing myself a password generator, one that takes a string (aka the URL of a website) and processes it into a secure password that can't be backtracked based on the name of the website.

                  在部分代码中,我创建了一个递归函数,如下所示:

                  In part of the code, I created a recursive function that looks like this:

                  def get_number(n = 0, nums = ''):
                      for i in range(0, len(url)):
                          #both n and nums are changed
                      if len(nums) < num_length:
                          get_number(n, nums)
                      else:
                          print(nums)
                          return(nums)
                  

                  ...

                  print(get_number())
                  

                  我希望'nums' 输出两次,因为我在 else 块中打印它并稍后打印返回.但是,如果它确实通过了递归循环,则从 else 块打印nums",并且该函数返回None".如果 if len(nums) <num_length 第一次为假,然后返回正确的值.

                  I would expect 'nums' to output twice, since I print it in the else block and print the return later on. But, if it does go through a recursive loop, 'nums' is printed from the else block and the function returns 'None'. If if len(nums) < num_length is false the first time, then it returns the proper value.

                  如果我验证它返回的对象实际上不是之前的行None",为什么它会返回None"?

                  Why would it return 'None', if I verified that the object it is returning is not in fact 'None' the line before?

                  我对 Python 有点陌生,他们处理递归的方式不同吗?

                  I'm a little new to Python, do they handle recursions differently?

                  感谢您的宝贵时间

                  问题已解决.忘记了递归调用的返回语句.谢谢:D

                  Problem fixed. Forgot a return statement on the recursive call. Thanks :D

                  推荐答案

                  我认为您在嵌套的 get_number 之前缺少一个 return.所以,它正在执行并返回,但你没有对递归值做任何事情.

                  I think you're missing a return before the nested get_number. So, it's executing and returning, but you aren't doing anything with the recursed value.

                  def get_number(n = 0, nums = ''):
                      for i in range(0, len(url)):
                          #both n and nums are changed
                      if len(nums) < num_length:
                          return get_number(n, nums)
                  
                      print(nums)
                      return nums
                  

                  这篇关于如何从递归 Python 函数中返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

                  <tfoot id='2zb7z'></tfoot>

                  <small id='2zb7z'></small><noframes id='2zb7z'>

                      <tbody id='2zb7z'></tbody>

                      <bdo id='2zb7z'></bdo><ul id='2zb7z'></ul>

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