1

I have this function which returns two values and I want to repeat the same with those returned values.

fPoint = getFirstPoint (x, y, currCalcX, currCalcY) 
newCurrValX, newCurrValY = fPoint
print(newCurrValX, newCurrValY)

Above function returns values like this:

250.0 60.0

I want to apply these values on currCalcX and currCalcY and repeat the function getFirstPoint() until None is returned.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
smc
  • 205
  • 2
  • 10

1 Answers1

3

Use a loop:

# dummy implementation - returns None if a or b is 0, ignores x and y
def getFirstPoint(x,y,a,b):
    if a==0 or b==0:
        return None
    return a-1,b-1

x, y, currCalcX, currCalcY = 0,0,4,4  # add sensible values here

while True:
    fPoint = getFirstPoint (x, y, currCalcX, currCalcY) 
    if fPoint is None:
        print("Done")
        break
    currCalcX, currCalcY = fPoint
    print(currCalcX, currCalcY)

Output:

3 3
2 2
1 1
0 0
Done

Recursion is not needed here - it stacks up on function stack frames needlessly. You might hit the recursion limit if it takes too many recursions (and it is not needed at all) - see What is the maximum recursion depth in Python, and how to increase it? for recursion limit explanations.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks, Let me try this. – smc Sep 07 '19 at 16:50
  • Not working. I'm getting same values over and over. Seems function is not running with updated values. – smc Sep 07 '19 at 16:56
  • @ShyamMohan fixed typo - you need to replace the values of `currCalcX, currCalcY` with the results - not create `newCalcX, newCalcY` – Patrick Artner Sep 07 '19 at 16:57
  • That gives an error `UnboundLocalError: local variable 'currCalcX' referenced before assignment ` – smc Sep 07 '19 at 17:06
  • @Shy fully running example edited. The error you get in your code is probably because you did not set `'currCalcX'` before using in in the function – Patrick Artner Sep 07 '19 at 17:10