鱼C论坛

 找回密码
 立即注册
查看: 1273|回复: 2

[已解决]请教为什么跟着视频输入还是出现错误?

[复制链接]
发表于 2022-6-24 12:30:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
fibonacci_cache = {}
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
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
注意缩进
  1. #!/usr/bin/env python
  2. #coding=utf-8

  3. fibonacci_cache = {}

  4. def fibonacci(n):
  5.     #If we have cached the value, then return it
  6.     if n in fibonacci_cache:
  7.         return fibonacci_cache[n]
  8.     #Compute the Nth term
  9.     if n == 1:
  10.         value = 1
  11.     elif n== 2:
  12.         value = 1
  13.     elif n > 2:
  14.         value = fibonacci(n-1) + fibonacci(n-2)
  15. #Cache the value and return it
  16.     fibonacci_cache[n] = value
  17.     return value
  18. for n in range(1,101):
  19.     print(n,":", fibonacci(n))
复制代码
CD9$LO9PXZYQ57{~)TTT.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-6-24 12:40:10 | 显示全部楼层    本楼为最佳答案   
注意缩进
  1. #!/usr/bin/env python
  2. #coding=utf-8

  3. fibonacci_cache = {}

  4. def fibonacci(n):
  5.     #If we have cached the value, then return it
  6.     if n in fibonacci_cache:
  7.         return fibonacci_cache[n]
  8.     #Compute the Nth term
  9.     if n == 1:
  10.         value = 1
  11.     elif n== 2:
  12.         value = 1
  13.     elif n > 2:
  14.         value = fibonacci(n-1) + fibonacci(n-2)
  15. #Cache the value and return it
  16.     fibonacci_cache[n] = value
  17.     return value
  18. for n in range(1,101):
  19.     print(n,":", fibonacci(n))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-24 14:09:43 | 显示全部楼层
可以了,谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 04:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表