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)

就可以自行并且没有错误了

每天进步一点点~~

原文地址:https://www.cnblogs.com/clairedandan/p/10923108.html

时间: 2024-08-06 10:41:30

python --RecursionError: maximum recursion depth exceeded in comparison的相关文章

python RecursionError: maximum recursion depth exceeded in comparison错误

处理快速排序,递归深度可能非常大,而系统默认的深度可能没有这么大 需要设置最大递归深度 1 import sys 2 sys.setrecursionlimit(100000) # 这个值的大小取决你自己,最好适中即可,执行完递归再降低,毕竟递归深度太大,如果是其他未知的操作引起,可能会造成内存溢出

Python递归报错:RuntimeError: maximum recursion depth exceeded in comparison

Python中默认的最大递归深度是989,当尝试递归第990时便出现递归深度超限的错误: RuntimeError: maximum recursion depth exceeded in comparison 简单方法是使用阶乘重现: 1 #! /usr/bin/env Python 2 3 def factorial(n): 4 5 if n == 0 or n == 1: 6 7 return 1 8 9 else: 10 11 return(n * factorial(n - 1)) >

RecursionError: maximum recursion depth exceeded while calling a Pytho

RecursionError:在调用Python对象时超过最大递归深度 项目 python flask: 在pycharm软件里, default settings --> Build, Execution, Deployment -> Python Debugger. 找到 "Gevent compatible" ,勾选, 打开settings 找到并点击 Build, Execution, Deployment,然后找到并点击Python Debugger,在这个对话款

pyinstaller打包报错: RecursionError: maximum recursion depth exceeded,UnicodeDecodeError 解决办法

出现原因:     这个错误意思是超过最大递归深度,python默认的递归深度默认是1000),因此当递归深度超过就会引发这样的异常. 解决方法: 1.执行pyinstaller -F XXX.py 它会在你的目录文件生成XXX.spec文件,然后报错,出现该类异常. 2.打开XXX.spec文件,在开头添加上面两行代码. import sys sys.setrecursionlimit(1000000) 3.继续执行打包,但是还文件名:pyinstaller -F XXX.spec ,执行该文

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度" 报错如下:   Traceback (most recent call last):   File "<stdin>", line 1, 

Odoo8查询产品时提示&quot;maximum recursion depth exceeded while calling a Python object&quot;

今天在生产系统中查询产品时,莫名提示错误:maximum recursion depth exceeded while calling a Python object,根据错误日志提示,发现在查询产品时,系统会构造一个domain,查询所有库位的库存量.当仓库较多的时候,构造的这个domain比较长,然后解析这个domain的方法distribute_negate是递归调用,因为递归次数太多,所以就提示错误. 根据源码查看了生成domain的条件,这个部分不太好调整,所以后来直接找了个方法来增加

python递归深度报错--RuntimeError: maximum recursion depth exceeded

当你的程序递归的次数超过999次的时候,就会引发RuntimeError: maximum recursion depth exceeded. 解决方法两个: 1.增加系统的递归调用的次数: import sys sys.setrecursionlimit(n) n为你想要的递归上限 2.优化代码,减少递归的次数. 原文地址:https://www.cnblogs.com/everfight/p/maximum_recursion.html

win10启动django项目报错 Django RuntimeError: maximum recursion depth exceeded

错误:Django RuntimeError: maximum recursion depth exceeded 原因出自Python\Lib\fuctools.py 把 convert = {    '__lt__': [('__gt__', lambda self, other: other < self),                ('__le__', lambda self, other: not other < self),                ('__ge__', 

函数递归时,递归次数到900多时,就是抛出异常exception RuntimeError(&#39;maximum recursion depth exceeded&#39;,)

import subprocess import multiprocessing import urllib import sys import os import pymongo import signal import time client=pymongo.MongoClient("192.168.139.143",27017) db=client.domaindb collection=db.domain def getdomain(i): print("proces