WheatBlock 发表于 2022-6-24 12:30:50

请教为什么跟着视频输入还是出现错误?

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'

人造人 发表于 2022-6-24 12:40:10

注意缩进
#!/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))

WheatBlock 发表于 2022-6-24 14:09:43

可以了,谢谢!
页: [1]
查看完整版本: 请教为什么跟着视频输入还是出现错误?