Zig Zag & Zoom (Python)[SOLVED]

Hey, I’m having trouble understanding how this code works.
I’ve completed the level (and the one before obviously which is similar).

But what is the code actually doing and how does it slow your character’s speed down?
I just don’t get it. Is (n) the character’s speed or time? something else completely?

The part I’m having the most trouble with is the while true loop at the bottom. I just don’t get what it is actually doing and how it affects my character and his speed.

Please simplify as I’m completely new to this.
Thanks in advance.

(Edited as requested for formatting)

# Escape from Death Valley!
# Move by with a zigzag pattern using real modulo functions.

# This function returns a value from 0 to 15:
def mod15(n):
    while n >= 15:
        n -= 15
    return n

# This function should return a value from 0 to 9:
def mod9(n):
    # While n is greater or equal to 9, subtract 9 from n:
    while n >= 9:
        n -= 9
    return n

# Don't change the following code:
while True:
    time = hero.time
    if time < 30:
        y = 10 + 3 * mod15(time)
        y = 20 + 3 * mod9(time)
    x = 10 + time
    hero.moveXY(x, y)
1 Like

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

@Psyko2k please format your code correctly using this button:
format code
we can’t help you until then. Thanks!



sorry i didn’t realise that thats how you format (i used quotes instead).
all done now.

many thanks.

1 Like

That should be


while n >= 9:
       n = n - 9 
    return n

See the difference other than that you are good to go.

No I did it his/her way and it’s fine but

This is what @Psyko2k is asking not help with the level

1 Like

@Psyko2k thanks for trying to understand as best you can. For this level, I don’t think you’re really supposed to understand it. It just shows you what you can do later with what you’re currently learning. So keep up the good work and you’ll get there!

1 Like


thanks for the response :slight_smile:

Fair enough,

i’ll see what future levels bring and hopefully somethig will click.

i’ll leave this on a little while before clicking solution incase somebody knows.

Thanks for the response :slight_smile:

1 Like

You learn that stuff in glacier I think. :+1: :slight_smile:

By the way, you know the time stuff at the bottom, well its hard coded to perfectly dodoge the missiles

Try changing the numbers here and you’ll see you character moves differently.

1 Like

Yeah it is funny considering that you later go on to not hard code levels

Hi Psyko2k,

Good question - it’s great that you’re trying to understand why code works :smile:.

With the functions, ‘n’ is whatever you set it to be. In this case we are calling the function with


so yes, here n is time (ie number of seconds).

Have a look at what is happening in the while True loop, and go through each step:

  • At time = 1, then y = 10 + (3 x 1) = 13 and x = 11. The hero moves to (11, 13).
  • At time = 2, then y = 10 + (3 x 2) = 16 and x = 12. The hero moves to (12, 16).
  • At time = 3, then y = 10 + (3 x 3) = 19 and x = 13. The hero moves to (13, 19).
  • etc
  • At time = 14, then y = 10 + (3 x 14) = 52 and x = 24. The hero moves to (24, 52).
  • At time = 15, then y = 10 + (3 x 0) = 10 and x = 25. The modulo function sets the hero’s y position back to the bottom of the screen. The hero moves to (25, 0). This takes a bit of time which is why the next few points are quite spread out as the computer waits for the hero to get there before doing the next loop.

The hero moves slowly because you’re telling them to move a tiny bit at a time - each move is really small, and then there’s a little pause as time increases. (The computer is calculating in smaller units than each second, so we don’t see these pauses).

Feel free to post with more questions if you want more of an explanation. If you haven’t used graphs before, then have a play around with something like Equation Grapher - start by putting in y=2x-2, and then change the equation to see what effect it has.



Thank you jenny, that’s an amazing explanation. You broke it down to its core piece by piece which is what I needed.

Also thanks to everyone else who answered. :slight_smile:


This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.