python - RecursionError: maximum recursion depth exceeded -
i'm working on building python script solve riemann's paradox. idea take inputted number, , add or subtract 1/2,1/3,1/4,1/5... to/from 1 until have inputted number. i'm running recursion error result of calling comparison , adding/subtracting classes every time comparison made.
my code follows:
import math #declare values of variables before referenced goal = float(input("what number trying achieve?")) current_number = float(1) negatives = (2) positives = (3) #----------------------------------------------------- def add(goal,current_number,positives,negatives): #define add operation when current number less goal current_number = current_number+(1/positives) #add current fraction current number positives = positives+2 #set next additional fraction comparison(goal,current_number,positives,negatives) #go comparision of current number goal def subtract(goal,current_number,positives,negatives): #define subtract operation when current number greater goal current_number = current_number-(1/negatives) #subtract current fraction current number negatives = negatives+2 #set next subtractional fraction comparison(goal,current_number,positives,negatives) #go comparision of current number goal def comparison(goal,current_number,positives,negatives): #define comparison between current number goal if current_number < goal: add(goal,current_number,positives,negatives) #if current number less goal, go add next fraction if current_number > goal: subtract(goal,current_number,positives,negatives) #if current number greater goal, subtract next fraction if current_number == goal: print("the equation number 1+1/3...") print("+1/",positives) print("...-1/2...") print("-1/",negatives) #if current number equal goal, print results comparison(goal,current_number,positives,negatives) #do comparison between current number , goal
i'm wondering can solve problem.
you don't need function add , subtract because adding negative same subtracting.
also have fixed code, calling function calls function called causing recursion error.
hope helps :)
import math goal = float(input("what number trying achieve?")) current_number = 0 fraction = 2 #----------------------------------------------------- def change(goal, current_number, fraction, sign = 1): #define add operation when current number less goal global positives current_number = current_number + (1/fraction) * sign # add or take away current fraction current number return current_number def comparison(goal, current_number, fraction): #define comparison between current number goal print("the equation number is:") print("1/2") while round(current_number, 3) != round(goal, 3): # don't have round if don't want fraction = fraction + 1 if current_number < goal: print("+1/"+str(fraction)) # positive current_number = change(goal, current_number, fraction) #if current number less goal, go add next fraction elif current_number > goal: print("-1/"+str(fraction)) # positive current_number = change(goal, current_number, fraction, -1) print("...") comparison(goal,current_number, fraction) #do comparison between current number , goal
Comments
Post a Comment