Table Of Contents

Previous topic

16. If only ...

Next topic

18. Listen to me ... or else ....

This Page

17. Finding the right spot

While the program you just wrote works for worlds Tokens 1 and Tokens 2, it will fail if you try it for world Tokens 3.

Try it!

Try your program with all three worlds.

17.1. Another condition

Note

at_goal(): green_home_tile house racing_flag

In addition to being able to find out if tokens are located at the position where Reeborg finds himself, Reeborg can also determine if he reached the coloured square which we described before as Reeborg’s home. In many worlds, it makes more sense to think of this as Reeborg’s goal destination, rather than home, and the function that Reeborg uses to determine this is at_goal(). Here’s the outline of a solution that should work in all four worlds mentioned above.

def move_until_done():
    if at_goal():
        # something
    move()
    if object_here():
        # something
        # something else
        # something else again

repeat 42:
    move_until_done()

Note

If you have the World info shown for the world Tokens 3, you will see that the suggested solution uses the Python keyword while which we have not see yet.

Complete the above (in the Python Code editor) and make sure it works for all three worlds mentioned above.

17.2. And now, something different

You did complete the above exercise, didn’t you? ... Good.

Do this!

Select either world Home 1 or Home 2. Would the same program you use for the Tokens world work? After you have determined this, try running it to confirm your understanding.

17.3. Hurdles race!

Have a look at worlds Hurdles 1 and Hurdles 2. Ignoring the end goal for a second, a program that Reeborg could follow to race over these hurdles would alternate between two instructions

  • move()
  • jump_over_hurdle()

with the appropriate definition for jump_over_hurdle(). If you could include a test (if statement) at some point to see if you have reached the goal, you could use the above to create a new function, that we could call move_and_jump_until_done() so that a program suitable for both worlds Hurdles 1 and Hurdles 2 would be:

repeat 42:
    move_and_jump_until_done()

Do it!

Write such a program and make sure it works.

Hint

Your program could look as follows:

from my_lib import turn_right

def move_and_jump_over_hurdle():
    # some instructions

repeat 42:
    move_and_jump_over_hurdle()
    if at_goal():
        done()

17.3.1. A question for you

Could this program work without changing anything for world Hurdles 3?