你为什么不做这样的事情:
>>> def fibonacci(number):
... if number < 2:
... return number
... print "Number is currently %d, getting fibonacci(%d)" % (number, number - 1)
... minus_one = fibonacci(number-1)
... print "Number is currently %d, just got fibonacci(%d), now getting fibonacci(%d)" % (number, number - 1, number - 2)
... minus_two = fibonacci(number-2)
... print "Number is currently %d, returning %d + %d" % (number, minus_one, minus_two)
... return minus_one + minus_two
所以当你打电话时,fibonacci
你会得到这样的东西:
>>> fibonacci(4)
Number is currently 4, getting fibonacci(3)
Number is currently 3, getting fibonacci(2)
Number is currently 2, getting fibonacci(1)
Number is currently 2, just got fibonacci(1), now getting fibonacci(0)
Number is currently 2, returning 1 + 0
Number is currently 3, just got fibonacci(2), now getting fibonacci(1)
Number is currently 3, returning 1 + 1
Number is currently 4, just got fibonacci(3), now getting fibonacci(2)
Number is currently 2, getting fibonacci(1)
Number is currently 2, just got fibonacci(1), now getting fibonacci(0)
Number is currently 2, returning 1 + 0
Number is currently 4, returning 2 + 1
3
它仍然很复杂,但至少现在您可以看到该函数正在做什么来计算您的数字。