帮忙看看这个python代码错在哪了?
发布网友
发布时间:2022-12-16 20:41
我来回答
共3个回答
热心网友
时间:2024-12-11 22:16
没有发现错误,可以运行啊,你是不是需要检查一下缩进
追问缩进是自动生成的,应该问题不大。下面是报的一堆错误。运行环境是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
追答
我在pycharm里又试了一下,确实是正常运行的
你可以打印一下数据类型,我这里都是int,你看一下你那里是不是出现了None
热心网友
时间:2024-12-11 22:16
>>> 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 <mole>
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'
热心网友
时间:2024-12-11 22:17
代码没问题,重新整理一下缩进就可以了