请教为什么跟着视频输入还是出现错误?
fibonacci_cache = {}def fibonacci(n):
#If we have cached the value, then return it
if n in fibonacci_cache:
return fibonacci_cache
#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 = value
return value
for n in range(1,101):
print(n,":", fibonacci(n))
提示 : value = fibonacci(n-1) + fibonacci(n-2)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' 注意缩进
#!/usr/bin/env python
#coding=utf-8
fibonacci_cache = {}
def fibonacci(n):
#If we have cached the value, then return it
if n in fibonacci_cache:
return fibonacci_cache
#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 = value
return value
for n in range(1,101):
print(n,":", fibonacci(n))
可以了,谢谢!
页:
[1]