Not an actual modulo operation?


#1


In this level in the desert where the return command is introduced (name displayed in screenshot: “Hetze nicht, sei still”) there’s a function referred to as “modulo operation” in the hints section:
modulo

However, the actual function is not performing a modulo calculation (calculating the rest of a division), there is only a subtraction:

# Diese Funktion gibt einen Wert >= 0 und <30 zurück:
def mod30(n):
    if n >= 30:
        return n - 30
    else:
        return n

It seems that someone just named that function mod30 for some reason that has nothing to do with what it does, right?
Also the comment says that the function will return a value between 0 and 29, which is not true for n > 60
This is all very confusing.
Are there errors in the design of this level/the hints text, or is it I just me getting everything wrong?


Zig zag and zoom
#2

Division is only a series of subtractions, just as multiplication is just a series of additions. For example, if n was equal to 300, then the code would be:

def mod30(300):
    if 300 is bigger than or equal to 30:
        return 300 - 30 (the returned number will now equal 270)
    else:
        return n ( but at the moment n(270) is still greater than or equal to 30 so we repeat the process again until n is less than 30 at which point the code will pass to the else and we will return the number n)

this is how the series of subtractions would go If 300 was n and we ran the above code:
300 - 30 = 270 ( back to the top of the code)
270 - 30 = 240( back again)
240 - 30 = 210

and so on until:
60 - 30 = 30
30 - 30 = 0 (30 is still greater than or equal to 30)

then we will pass to the else and return n, which is now 0

As you can see, this repeated subraction is really the same as division, so this is a modular operation.

Here’s a link to the Wikipedia page explaining modulo in more depth https://en.wikipedia.org/wiki/Modulo_operation


#3

Thank you, this explanation was very helpful - it helped me understand this level’s code (not in its entirety, but the part that you explained).
However, I feel it’s worth mentioning that waht you said isn’t true for the snippet of code I posted alone!
It only works along with the rest of the pre-written code of that level.
Here it is:

# Diese Funktion gibt einen Wert >= 0 und <30 zurück:
def mod30(n):
    if n >= 30:
        return n - 30
    else:
        return n

# Diese Funktion sollte einen Wert >=0 und <40 zurückgeben:
def mod40(n):
    # Benutze ein if-Statement, um den korrekten Wert zurückzugeben.
    if n >= 40:
        return n - 40
    else:
        return n

# Ändere den folgenden Code nicht:
while True:
    time = hero.time
    x = mod30(time) + 25
    y = mod40(time) + 10
    hero.moveXY(x, y)

i.e. either of the two mod(n) functions only becomes a modulo operation when repeatedly called upon in a loop!
By itself it is indeed just a subtraction.
that’s also how the comment is meant:

# this function returns a value >= 0 and <30 , _when its looped_
def mod30(n):
    if n >= 30:
        return n - 30
    else:
        return n

…The part I don’t understand is how this movement pattern helps the hero evade the cannons - and why a modulo operation is needed to achieve it. I’m guessing you have to be one of the people who wrote this level to understand why.


#4

Yeah, I think you’re right or maybe if it sees that you’ve but the right code in, it says you’ve done the level.