python --RecursionError: maximum recursion depth exceeded in comparison
在学习汉娜塔的时候,遇到一个error RecursionError: maximum recursion depth exceeded in comparison
经过百度,百度的方法:
加上:
import sys
sys.setrecursionlimit(100000)
可是我加上之后结果如下,并没有解决问题,python还提示意外退出:
1、再此经过思考(也不是思考,再从头看了学习视频,添加了两个return None,问题解决✌️✌️💕)
python 函数要么返回预期的值,要么返回None
2、注意点,递归要有结束的条件:如下
1 def fun_a(n): 2 #print(n) 3 #if n == 1: 4 #return 1 5 return n*fun_a(n-1) 6 rst=fun_a(5) 7 print(rst)
Traceback (most recent call last): File "/Users/fudandan/Desktop/hello/11/diaoyong.py", line 6, in <module> rst=fun_a(5) File "/Users/fudandan/Desktop/hello/11/diaoyong.py", line 5, in fun_a return n*fun_a(n-1) File "/Users/fudandan/Desktop/hello/11/diaoyong.py", line 5, in fun_a return n*fun_a(n-1) File "/Users/fudandan/Desktop/hello/11/diaoyong.py", line 5, in fun_a return n*fun_a(n-1) [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded [Finished in 0.1s with exit code 1]
结束条件加上:
1 def fun_a(n): 2 #print(n) 3 if n == 1: 4 return 1 5 return n*fun_a(n-1) 6 rst=fun_a(5) 7 print(rst)
就可以自行并且没有错误了
每天进步一点点~~
每天进步一点点~~