2015-10-26 56 views
0

這裏是我的代碼:當我希望得到詮釋,但外部函數返回無

def fibonacci(t0, t1, b, n): 
    t2 = t1**2 + t0 
    t0 = t1 
    t1 = t2 
    b += 1 
    if (n > b): 
     fibonacci(t0, t1, b, n) 
    else: 
     return t2 

...(定義T0,T1,B,N) FB =斐波納契(T0,T1,B ,n)

但fb =無。爲什麼t2不返回?

+1

'如果(N> B): 返回斐波納契(T0,T1,B,N)' – inspectorG4dget

回答

1
if (n > b): 
    fibonacci(t0, t1, b, n) 
else: 
    return t2 

兩個分支都需要return聲明。遞歸調用函數不會自動將返回值傳遞到調用堆棧的頂部。

if (n > b): 
    return fibonacci(t0, t1, b, n) 
else: 
    return t2 
相關問題