Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数返回后如何打印语句?_Python_Python 3.x_Return - Fatal编程技术网

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

Python 函数返回后如何打印语句?,python,python-3.x,return,Python,Python 3.x,Return,以下是我目前拥有的代码: 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,'val

以下是我目前拥有的代码:

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语句。

return
语句之后的任何内容都不会被执行。return语句立即终止函数并“返回”给调用者。所以这真的不可能

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()
    F_time==t1-t
    print ('It took',F_time,'seconds to sort',n,'values using recursion')
    return t
但是有两种方法可以得到(几乎)相同的结果

方法#1: 执行return语句所需的时间几乎可以忽略不计。因此,您可以将必要的基准代码移到return之上,并获得几乎相同的结果,就好像它实际上是在“return”语句之后执行的一样

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()
    F_time==t1-t
    print ('It took',F_time,'seconds to sort',n,'values using recursion')
    return t
方法2: 将计时代码置于函数之外,这是首选方法

def F(n):

    if n==0:
        return (0)
    elif n==1:
        return (1)
    else:
        return (F(n-1)+F(n-2))
    return t

t=time.time()
F(5)
t1==time.time()
F_time==t1-t
print ('It took',F_time,'seconds to sort',5,'values using recursion')

希望这有帮助,祝你好运

为了在函数中的
return
语句之后真正执行操作,请使用
try finally
如下所示:

# 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 %.2fµs seconds" % (n, F_time * 1e6))

if __name__ == '__main__':
    print("RESULT:", F(4))
输出:

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

注意:在这个例子中,n>1的计时当然包括在嵌套递归调用中打印到stdout的时间,在这个小例子中,这就决定了计时。

非常感谢,确实如此@很高兴知道!如果答案是你想要的,别忘了接受完整的答案:)这有点老套,但嘿,它行得通!我正在考虑产生一个新的线程。但这很有魅力。