【发布时间】:2019-03-25 04:02:23
【问题描述】:
我是编码和学习 python 的新手。我有作业要打印斐波那契 使用称为 Memoization 的方法获得 N = 11 和 N = 200 的数字。我找到了解决方案,但是当我运行代码时,我得到了两件事:1。
Traceback (most recent call last):
**File "python", line 7
if n== 1:
^**
**IndentationError: unexpected indent**
第二个我在跑步时得到了空的结果。代码有什么问题:
def fibonacci (n) :
# If we have cached the value, then return it
if n in fibonacci_cache:
return fibonacci_cache[n]
# Compute the Nth term
if n== 1:
value = 1
elif n == 2:
value = 1
elif n > 2:
value = fibonacci(n-1) + fibonacci(n-2)
# Cache the value and return it
fibonacci_cache[n] = value
return value
print(n, ":", fibonacchi(11))
【问题讨论】:
-
".. 第二个我在运行的时候得到了空结果.." 但是你刚才说你的代码没有在运行?
-
缩进在python中非常重要,你的 if n==1 必须从行首开始。
-
您似乎已经“找到”了代码here。我怀疑作业的重点是让您自己编写代码,而不是抄袭它。
-
按照错误提示修复缩进。
标签: python arrays indentation fibonacci