【问题标题】:Python recursive function returning none after completionPython递归函数完成后不返回任何内容
【发布时间】:2014-04-18 20:47:10
【问题描述】:

我的代码应该从 n 倒计时到 1。代码完成但最后返回 None。关于为什么会发生这种情况以及如何解决它的任何建议?提前致谢!

def countdown(n):

    '''prints values from n to 1, one per line
    pre: n is an integer > 0
    post: prints values from n to 1, one per line'''

    # Base case is if n is <= 0
    if n > 0:
        print(n)
        countdown(n-1)
    else:
        return 0

def main():

    # Main function to test countdown
    n = eval(input("Enter n: "))
    print(countdown(n))

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python-3.x recursion


    【解决方案1】:

    print(countdown(n)) 打印countdown(n) 返回的值。如果n &gt; 0,则返回值为None,因为如果没有执行return语句,Python函数默认返回None。

    由于您在 countdown 中打印值,因此修复代码的最简单方法是简单地删除 main() 中的 print 调用:

    def countdown(n):
    
        '''prints values from n to 1, one per line
        pre: n is an integer > 0
        post: prints values from n to 1, one per line'''
    
        # Base case is if n is <= 0
        if n > 0:
            print(n)
            countdown(n-1)
    
    def main():
    
        # Main function to test countdown
        n = eval(input("Enter n: "))
        countdown(n)
    
    if __name__ == '__main__':
        main()
    

    编辑:删除了else-clause,因为文档字符串说打印的最后一个值是 1,而不是 0。

    【讨论】:

    • 非常感谢!这绝对有帮助。
    【解决方案2】:

    删除 print(countdown(n)) 并将其替换为 countdown(n)

    当 n 大于 0 时,您的倒计时函数不返回任何内容,因此 print 语句正在打印 None。您也可以删除 countdown(n) 中的 else 语句 - 因为您从不关心 countdown 的返回值,所以它没有任何用处。

    def countdown(n):
    
        '''prints values from n to 1, one per line
        pre: n is an integer > 0
        post: prints values from n to 1, one per line'''
    
        # Base case is if n is <= 0
        if n > 0:
            print(n)
            countdown(n-1)
        # No need to return a value from here ever - unused in the recursion and
        # you don't mention wanting to have zero printed.
        #else:
        #    return 0
    
    def main():
    
        # Main function to test countdown
        n = eval(input("Enter n: "))
    
        # Don't print the result of countdown - just call it
        #print(countdown(n))
        countdown(n)
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      【解决方案3】:

      您想将递归调用返回到countdown 的下一次调用:

      def countdown(n):
      
          '''prints values from n to 1, one per line
          pre: n is an integer > 0
          post: prints values from n to 1, one per line'''
      
          # Base case is if n is <= 0
          if n > 0:
              print(n)
              return countdown(n-1)
          else:
              return 0
      
      def main():
      
          # Main function to test countdown
          n = eval(input("Enter n: "))
          print(countdown(n))
      
      if __name__ == '__main__':
          main()
      

      这样,当达到基本情况时,返回值 0 应该向上传播并返回堆栈。

      不幸的是,由于您的基本情况返回 0,这将导致打印 0。但是,如果您想返回一些东西(考虑在这种情况下您是否真的需要),这就是您的做法它。

      如果你不需要返回任何东西,那么你就不需要打印结果。进一步修改

      print(countdown(n))
      

      countdown(n)
      

      【讨论】:

        猜你喜欢
        • 2021-07-23
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 2013-09-01
        • 2016-02-15
        • 2021-01-29
        • 1970-01-01
        • 2016-06-30
        相关资源
        最近更新 更多