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

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

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

        <tfoot id='GKtI1'></tfoot>
        <legend id='GKtI1'><style id='GKtI1'><dir id='GKtI1'><q id='GKtI1'></q></dir></style></legend>

        如何在函数中返回后打印语句?

        时间:2023-09-02

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

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

                  本文介绍了如何在函数中返回后打印语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是我目前拥有的代码:

                  Here is the code as I currently have it:

                  def F(n):
                      t=time.time()
                      if n==0:
                          return (0)
                      elif n==1:
                          return (1)
                      else:
                          return (F(n-1)+F(n-2))
                      t1==time.time()
                      return t
                      F_time==t1-t
                      print ('It took',F_time,'seconds to sort',n,'values using recursion')
                  

                  我正在尝试打印n个斐波那契数列并取前后的时间,但是由于return语句,它不会接受return之后的变量,也不接受下面的print语句.

                  I'm trying to print n numbers of the Fibonacci sequence and take the time before and after, but because of the return statements, it won't accept the variable after the return, nor the following print statement.

                  推荐答案

                  为了真正执行函数内部 return 语句之后的事情,请使用 try-finally 像这样:

                  In order to really execute things after the return statement(s) inside a function use try-finally like this:

                  # platform independent high-resolution clock
                  from timeit import default_timer as timer  
                  
                  def F(n):
                      t = timer()
                      try:
                          if n == 0:
                              return 0
                          elif n == 1:
                              return 1
                          else:
                              return (F(n - 1) + F(n - 2))
                      finally:
                          t1 = timer()
                          F_time = t1 - t
                          print(u"F(%s) took %.2fs seconds" % (n, F_time * 1e6))
                  
                  if __name__ == '__main__':
                      print("RESULT:", F(4))
                  

                  输出:

                  F(1) took 0.92s seconds
                  F(0) took 0.84s seconds
                  F(2) took 429.78s seconds
                  F(1) took 0.84s seconds
                  F(3) took 520.12s seconds
                  F(1) took 0.67s seconds
                  F(0) took 0.75s seconds
                  F(2) took 90.85s seconds
                  F(4) took 700.82s seconds
                  RESULT: 3
                  

                  注意:在这个例子中,n > 1 的时间当然包括在嵌套递归调用中打印到标准输出的时间,然后在那个简单的例子中支配时间.

                  Note: in that example here the timings for n > 1 of course include time for printing to stdout in nested recursive calls, which then dominate timing in that trivial example.

                  这篇关于如何在函数中返回后打印语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么我不能将收益与回报一起使用? 下一篇:如何从递归 Python 函数中返回一个值?

                  相关文章

                  <small id='5djEO'></small><noframes id='5djEO'>

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