RecursionError:超出最大递归深度

时间:2016-11-10 16:17:44

标签: python math recursion

我正在构建一个解决Riemann悖论的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

我想知道我能做些什么来解决这个问题。

2 个答案:

答案 0 :(得分:0)

  1. current_number == goal中您要比较浮点数,使用==通常不是the right way to do it

  2. 您可能需要increase the maximum recursion depth

  3. 添加print(current_number)作为comparison()函数的第一行,看看您的current_number如何收敛/偏离目标。

答案 1 :(得分:0)

您不需要添加和减去函数,因为添加负数与减去相同。

此外,我修复了你的代码,调用一个函数来调用你调用它的函数是导致你的递归错误的函数。

  

希望这可以帮助你:)

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