# Zig Zag & Zoom (Python)\[SOLVED\]

**URL:** <https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483>\
**Category:** Level Help\
**Created:** [February 18, 2021, 12:40am UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483 "2021-02-18T00:40:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [February 18, 2021, 12:40am UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/1 "2021-02-18T00:40:35Z")

</div>

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)

```python
# 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)
    else:
        y = 20 + 3 * mod9(time)
    x = 10 + time
    hero.moveXY(x, y)

```

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [February 18, 2021, 1:32am UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/2 "2021-02-18T01:32:48Z")

</div>

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](https://discourse.codecombat.com/t/welcome-to-codecombat-discourse-forums/15601), which shows all essentials of this board! Thanks!

@Psyko2k please format your code correctly using this button:  
 ![format code](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/c/2/c2b2ed7eff737cd5b77dc85cdb08663122b24a8b.png)  
we can’t help you until then. Thanks!

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [February 18, 2021, 9:05pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/3 "2021-02-18T21:05:46Z")

</div>

Hey,

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

many thanks.

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 18, 2021, 9:17pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/4 "2021-02-18T21:17:18Z")

</div>

That should be

> [@Psyko2k](#):
>
> ```python
> while n >= 9:
> n -= 9
> return n
> 
> ```

This

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

```

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 18, 2021, 9:17pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/5 "2021-02-18T21:17:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [February 18, 2021, 9:51pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/6 "2021-02-18T21:51:54Z")

</div>

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

> [@Psyko2k](#):
>
> 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?

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

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [February 18, 2021, 10:17pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/7 "2021-02-18T22:17:33Z")

</div>

@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!  
~Andrew

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [February 18, 2021, 10:42pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/8 "2021-02-18T22:42:26Z")

</div>

exactly,

thanks for the response 🙂

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [February 18, 2021, 10:44pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/9 "2021-02-18T22:44:14Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![CocoCharlie](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cococharlie/32/19081_2.png) [@CocoCharlie](https://discourse.codecombat.com/u/CocoCharlie)\
**Post date:** [February 18, 2021, 10:52pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/10 "2021-02-18T22:52:14Z")

</div>

You learn that stuff in glacier I think. 👍 🙂

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [February 18, 2021, 11:07pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/11 "2021-02-18T23:07:09Z")

</div>

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

> [@Psyko2k](#):
>
> ```python
> < 30:
> y = 10 + 3 * mod15(time)
> else:
> y = 20 + 3 * mod9(time)
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [February 18, 2021, 11:15pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/12 "2021-02-18T23:15:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [February 19, 2021, 9:27am UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/13 "2021-02-19T09:27:01Z")

</div>

Hi Psyko2k,

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

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

```python
mod15(time)

```

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](https://www.mathsisfun.com/data/grapher-equation.html) - start by putting in y=2x-2, and then change the equation to see what effect it has.

Jenny

---

<div class="post-metadata">

**Author:** ![Psyko2k](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/psyko2k/32/21180_2.png) [@Psyko2k](https://discourse.codecombat.com/u/Psyko2k)\
**Post date:** [February 19, 2021, 9:24pm UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/14 "2021-02-19T21:24:03Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [February 20, 2021, 9:24am UTC](https://discourse.codecombat.com/t/zig-zag-zoom-python-solved/27483/15 "2021-02-20T09:24:52Z")

</div>

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