帮忙看看这个python代码错在哪了?
#coding=utf-8m={0:0,1:1}deffib(n):ifnotninm:m[n]=fib(n-1)+fib(n-2)returnm[n]if__name_...
# coding=utf-8
m = {0:0,1:1}
def fib(n):
if not n in m:
m[n] = fib(n-1) + fib(n-2)
return m[n]
if __name__ == "__main__":
f = fib(10)
print(f) 展开
m = {0:0,1:1}
def fib(n):
if not n in m:
m[n] = fib(n-1) + fib(n-2)
return m[n]
if __name__ == "__main__":
f = fib(10)
print(f) 展开
3个回答
追问
缩进是自动生成的,应该问题不大。下面是报的一堆错误。运行环境是pycharm2018版本的
Traceback (most recent call last):
File "D:/lq/Fibonacci_recursion.py", line 12, in
f = fib(10)
File "D:/lq/Fibonacci_recursion.py", line 8, in fib
m[n] = fib(n-1) + fib(n-2)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
Process finished with exit code 1
展开全部
>>> m = {0:0,1:1}
>>> m
{0: 0, 1: 1}
>>> def fib(n):
if not n in m:
m[n] = fib(n-1) + fib(n-2)
return m[n]
SyntaxError: expected an indented block
>>> def fib(n):
if not n in m:
m[n] = fib(n-1) + fib(n-2)
return m[n]
>>> if __name__ == "__main__":
f = fib(10)
print(f)
Traceback (most recent call last):
File "<pyshell#6>", line 2, in <module>
f = fib(10)
File "<pyshell#4>", line 3, in fib
m[n] = fib(n-1) + fib(n-2)
File "<pyshell#4>", line 3, in fib
m[n] = fib(n-1) + fib(n-2)
File "<pyshell#4>", line 3, in fib
m[n] = fib(n-1) + fib(n-2)
[Previous line repeated 6 more times]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
>>> m
{0: 0, 1: 1}
>>> def fib(n):
if not n in m:
m[n] = fib(n-1) + fib(n-2)
return m[n]
SyntaxError: expected an indented block
>>> def fib(n):
if not n in m:
m[n] = fib(n-1) + fib(n-2)
return m[n]
>>> if __name__ == "__main__":
f = fib(10)
print(f)
Traceback (most recent call last):
File "<pyshell#6>", line 2, in <module>
f = fib(10)
File "<pyshell#4>", line 3, in fib
m[n] = fib(n-1) + fib(n-2)
File "<pyshell#4>", line 3, in fib
m[n] = fib(n-1) + fib(n-2)
File "<pyshell#4>", line 3, in fib
m[n] = fib(n-1) + fib(n-2)
[Previous line repeated 6 more times]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码没问题,重新整理一下缩进就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询