Recursion Depth Exceeded

Not new to programming but new to Vex Code IQ and could use some help finding out why, on the third try to grab the correct color block I get a Max Recursion Error on this code. Please help!

Begin project code

def approach_block():
while not optical_9.is_near_object():
drivetrain.drive(FORWARD)
determine_color()

def determine_color():
drivetrain.drive(FORWARD)
wait(.5, SECONDS)
drivetrain.stop()
touchled_4.set_color(optical_9.color())
if optical_9.color()==Color.RED:
brain.play_sound(SoundType.TADA)
grab_block()
else:
come_back(False)

def grab_block():
drivetrain.drive(FORWARD)
wait(.5, SECONDS)
Claw.spin(REVERSE)
wait(.75, SECONDS)
drivetrain.stop()
come_back(True)

def come_back(has_block):
drivetrain.drive(REVERSE)
wait(2, SECONDS)
if has_block:
brain.play_sound(SoundType.FILLUP)
brain.program_stop()
else:
drivetrain.stop()
wait(2, SECONDS)
approach_block()

approach_block()

1 Like

That error is when a function calls itself infinitely. Here is an arcticle that might help you.

Your code never breaks. See the crude picture, because this is what your Vex brain is seeing, but there is no way out. It will run forever. It likely runs for a bit the first time you call it. There is a limit to how many times you can call the same function through a recursion which is where a function calls another as VEX_IQ_R12 has provided an article about.

Break this recursive loop and just call it as you need it rather than looping infinitely.

3 Likes

I dont know how that code functions. If you tried that in normal python you are getting an error. First you need to make is so that if there is a function in a function, make sure the first one is defined first. For example, the approach function needs to be after the determine color function. This makes sure that it will work 100%. To stop max recursion make it so that the last function doesnt call the first, or make it where something has to be true to trigger the first one. Hope that helps!

2 Likes