Question on the Maze Solver Activity

Good afternoon!
I have a question regarding the Maze Solver Activity. If the solution, there is a if/elif/else statement. I understand the logic of the statement however, as it applies to the Maze Solver, the if handles the left sensor, the elif handles the right but the else is a wait command. Why does there need to be a wait command with the else? There seems to already be a wait with the for value in range and a wait for the while loop. Why is a wait needed with the else? Hope this makes sense? I’ve attached a screenshot.
Thanks Michelle

1 Like

Hi Michelle,

You are absolutely right. The whole else section (where you highlighted yellow) can be omitted.

On the side note, it is never a bad idea to have include split of seconds on wait to ensure the sensor value gets updated.

Hope this helps.

1 Like

Thanks Jimmy. I guess it’s there because otherwise, there would need to be two if’s instead to test both sensors, which I assume is what they are trying to avoid. I could test the left sensor and if that’s false, have it turn right as a result though without the two if’s. I want to be able to apply the concept of if/elif/else to a project and I thought this one was a good one since it builds on the idea of sensors and algorithms. It’s just that the third option of the else with the wait command that is confusing since it isn’t necessary to run the code. Your explanation of using it to update the sensor is a good reason to offer them that I didn’t think of it before.

Thanks so much.
:slight_smile: Michelle

2 Likes

Just want to make sure I understand correctly. This would work in other cases. But in the example case (from the screenshot), there might be cases that neither left distance sensor nor right distance sensor got more than 300 mm. If this is the situation, the robot shouldn’t turn left nor right. This is the reason why we should use if/elif in this situation instead of if/else.

And Yes for this. For a more complexed program or a more “resource/time sensitive” case, the robot can eliminate the resource needed to test the right distance sensor when the left distance sensor is more than 300mm. So the program and the robot can run a little faster.

I’m happy to answer/help more!

2 Likes

Thanks Jimmy- I’m set! Appreciate all the help.
:slight_smile: Michelle

2 Likes