• <bdo id='eFYvA'></bdo><ul id='eFYvA'></ul>

        <i id='eFYvA'><tr id='eFYvA'><dt id='eFYvA'><q id='eFYvA'><span id='eFYvA'><b id='eFYvA'><form id='eFYvA'><ins id='eFYvA'></ins><ul id='eFYvA'></ul><sub id='eFYvA'></sub></form><legend id='eFYvA'></legend><bdo id='eFYvA'><pre id='eFYvA'><center id='eFYvA'></center></pre></bdo></b><th id='eFYvA'></th></span></q></dt></tr></i><div id='eFYvA'><tfoot id='eFYvA'></tfoot><dl id='eFYvA'><fieldset id='eFYvA'></fieldset></dl></div>
        <legend id='eFYvA'><style id='eFYvA'><dir id='eFYvA'><q id='eFYvA'></q></dir></style></legend><tfoot id='eFYvA'></tfoot>
      1. <small id='eFYvA'></small><noframes id='eFYvA'>

        Python 函数返回 None (检查了所有简单的解决方案并且它们不起作用)

        时间:2023-09-02

        • <bdo id='SS8nI'></bdo><ul id='SS8nI'></ul>

          • <legend id='SS8nI'><style id='SS8nI'><dir id='SS8nI'><q id='SS8nI'></q></dir></style></legend>
              1. <small id='SS8nI'></small><noframes id='SS8nI'>

                <tfoot id='SS8nI'></tfoot>

                  <i id='SS8nI'><tr id='SS8nI'><dt id='SS8nI'><q id='SS8nI'><span id='SS8nI'><b id='SS8nI'><form id='SS8nI'><ins id='SS8nI'></ins><ul id='SS8nI'></ul><sub id='SS8nI'></sub></form><legend id='SS8nI'></legend><bdo id='SS8nI'><pre id='SS8nI'><center id='SS8nI'></center></pre></bdo></b><th id='SS8nI'></th></span></q></dt></tr></i><div id='SS8nI'><tfoot id='SS8nI'></tfoot><dl id='SS8nI'><fieldset id='SS8nI'></fieldset></dl></div>
                    <tbody id='SS8nI'></tbody>
                  本文介绍了Python 函数返回 None (检查了所有简单的解决方案并且它们不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  现在,我已经为 Python(2.7 版)编写了二进制搜索.有时,它工作得很好,但有时它返回 None,尽管搜索的值在数组中.我已经尝试了每一种简单的方法来解决这个问题:我检查了函数返回的变量是否已定义,是否执行了 return 语句所在的工作流分支.并且:变量定义,分支执行.

                  Now, I have written a binary search for Python (version 2.7). Sometimes, it works just fine, but at other times it returns None although the searched value is in the array. I have tried every trivial way of fixing this: I have checked whether the variable the function returns is defined, whether the branch of the workflow in which the return statement is located is executed. And: the variable is defined, the branch is executed.

                  代码如下:

                  def binarySearch( array, desiderata, iMin, iMax ):
                  # Returns the index of the first instance of what we search
                  print 'min'
                  print iMin
                  print 'max'
                  print iMax
                  
                  # If our search array is empty
                  if ( iMin > iMax ):
                      return None
                  
                  midP = (iMin + iMax)/2
                  curre = tapeNr( array[midP][TAPE_NUMBER] )
                  final = tapeNr( desiderata )
                  print 'curre'
                  print curre
                  print 'final'
                  print final
                  print 'midP'
                  print midP
                  
                  if ( curre < final ):
                      # print midP
                      print "t: " + array[midP][TAPE_NUMBER] + ", d: " + desiderata
                      binarySearch( array, desiderata, midP + 1, iMax )
                  
                  else:
                      if ( curre > final ):
                          # print midP
                          print "t: " + array[midP][TAPE_NUMBER] + ", d: " + desiderata
                          binarySearch( array, desiderata, iMin, midP - 1 )
                  
                      else: 
                          print 'hooray'
                          # Now, find the first occurence of the value I need
                          i = midP
                          while ( array[i][TAPE_NUMBER] == desiderata ):
                              i -= 1
                              print i
                          print (i + 1)
                          return (i + 1)
                  

                  由于我的调试,有很多打印"语句.最后一个,'print (i+1)',实际上打印 (!) 我需要的东西的索引值,但函数仍然返回 None.

                  There are a lot of 'print' statements because of my debugging. The last of them, 'print (i+1)', actually prints (!) the index value of the thing I need, but the function still returns None.

                  你知道问题的根源吗?

                  推荐答案

                  你忽略了递归调用的返回值:

                  You ignore the return value of recursive calls:

                  binarySearch( array, desiderata, midP + 1, iMax )
                  

                  binarySearch( array, desiderata, iMin, midP - 1 )
                  

                  所以当 curre <最终:

                  if ( curre < final ):
                      # print midP
                      print "t: " + array[midP][TAPE_NUMBER] + ", d: " + desiderata
                      binarySearch( array, desiderata, midP + 1, iMax )
                  

                  你调用 binarySearch() 之后你的函数结束.如果没有显式返回,则意味着您的函数返回值设置为 None.

                  you call binarySearch() after which your function ends. Without an explicit return that means your function return value is set to None instead.

                  在这些行中添加 return 语句:

                  Add return statements to those lines:

                  return binarySearch( array, desiderata, midP + 1, iMax )
                  
                  # ...
                  
                  return binarySearch( array, desiderata, iMin, midP - 1 )
                  

                  这篇关于Python 函数返回 None (检查了所有简单的解决方案并且它们不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我需要帮助来理解使用 Python 的 return 语句及其在此递归语句中的作用 下一篇:python返回函数

                  相关文章

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