python --RecursionError: maximum recursion depth exceeded in comparison
Posted clairedandan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
就可以自行并且没有错误了
每天进步一点点~~
以上是关于python --RecursionError: maximum recursion depth exceeded in comparison的主要内容,如果未能解决你的问题,请参考以下文章
RecursionError:最大递归深度超出了python属性getter setter [重复]
python --RecursionError: maximum recursion depth exceeded in comparison
python RecursionError: maximum recursion depth exceeded in comparison错误
python RecursionError: maximum recursion depth exceeded in comparison错误