RecursionError:超出最大递归深度
Posted
技术标签:
【中文标题】RecursionError:超出最大递归深度【英文标题】:RecursionError: maximum recursion depth exceeded 【发布时间】:2017-03-24 18:08:33 【问题描述】:我正在构建一个 Python 脚本来解决黎曼悖论。这个想法是取一个输入的数字,然后将 1/2,1/3,1/4,1/5... 与 1 相加或减去,直到您输入数字为止。由于每次进行比较时回调我的比较并添加/减去类,我遇到了递归错误。
我的代码如下:
import math
#declare values of variables before they are referenced
goal = float(input("What number are you trying to achieve?"))
current_number = float(1)
negatives = (2)
positives = (3)
#-----------------------------------------------------
def add(goal,current_number,positives,negatives): #define the add operation for when the current number is less than the goal
current_number = current_number+(1/positives) #add current fraction to the current number
positives = positives+2 #Set the next additional fraction
comparison(goal,current_number,positives,negatives) #go to comparision of the current number to the goal
def subtract(goal,current_number,positives,negatives): #define the subtract operation for when the current number is greater than the goal
current_number = current_number-(1/negatives) #subtract current fraction from the current number
negatives = negatives+2 #set the next subtractional fraction
comparison(goal,current_number,positives,negatives) #go to comparision of the current number to the goal
def comparison(goal,current_number,positives,negatives): #define comparison between the current number to the goal
if current_number < goal:
add(goal,current_number,positives,negatives) #if the current number is less than the goal, go to add the next fraction to it
if current_number > goal:
subtract(goal,current_number,positives,negatives) #if the current number is greater than the goal, do to subtract the next fraction from it
if current_number == goal:
print("The equation for your number is 1+1/3...")
print("+1/",positives)
print("...-1/2...")
print("-1/",negatives)
#if the current number is equal to the goal, print the results
comparison(goal,current_number,positives,negatives) #do the comparison between the current number and the goal
我想知道我能做些什么来解决这个问题。
【问题讨论】:
程序可能永远不会从 'comparison()' 中的第一个 if 语句返回。您不是返回一个值,而是递归回原始函数。从加/减函数返回值。不要回忆调用它的函数。 @fjorn1 这是一个有趣的悖论,起初我对它的工作原理感到困惑,但现在我想我已经设法修复了代码:) 【参考方案1】:加减法不需要函数,因为加负数与减法相同。
我还修复了您的代码,调用了一个函数,该函数调用了您调用它的函数是导致您的递归错误的原因。
希望对你有帮助:)
import math
goal = float(input("What number are you trying to achieve?"))
current_number = 0
fraction = 2
#-----------------------------------------------------
def change(goal, current_number, fraction, sign = 1): #define the add operation for when the current number is less than the goal
global positives
current_number = current_number + (1/fraction) * sign # add or take away current fraction to the current number
return current_number
def comparison(goal, current_number, fraction): #define comparison between the current number to the goal
print("The equation for your number is:")
print("1/2")
while round(current_number, 3) != round(goal, 3): # you don't have to round if you don't want to
fraction = fraction + 1
if current_number < goal:
print("+1/"+str(fraction)) # positive
current_number = change(goal, current_number, fraction) #if the current number is less than the goal, go to add the next fraction to it
elif current_number > goal:
print("-1/"+str(fraction)) # positive
current_number = change(goal, current_number, fraction, -1)
print("...")
comparison(goal,current_number, fraction) #do the comparison between the current number and the goal
【讨论】:
总和 1 + 1/2 + 1/3 + ... 是无限的。你说他们越来越接近 2 是错误的。 谢谢!我会花一些时间来解决这个问题......我最近才重新开始使用 Python,你会惊讶于几年后你忘记了多少。【参考方案2】:在current_number == goal
中,您正在比较浮点数并且使用==
通常不是the right way to do it。
您可能想increase the maximum recursion depth。
将print(current_number)
添加为comparison()
函数的第一行,看看您的current_number
如何收敛到目标/偏离目标。
【讨论】:
…或者您可能想要使用列表并将代码重写为迭代而不是递归。以上是关于RecursionError:超出最大递归深度的主要内容,如果未能解决你的问题,请参考以下文章
RecursionError:重命名列条目后调用 Python 对象时超出最大递归深度
RecursionError:最大递归深度超出了python属性getter setter [重复]