Modulo Calculations

I’m going though Steelclaw Gap trying to understand using modulo to wrap around arrays and the way it’s working seems to be giving me the wrong answers.

def summonTroops():
    hero.say(len(hero.built) + " % " + len(summonTypes) + " = " + len(hero.built) % len(summonTypes))
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold >= hero.costOf(type):
        hero.summon(type)

As an example, the hero say code tells me that 1 % 8 = 1 and 2 % 8 = 2 but I’m sure this is wrong and online modulo calculators agree. 1 goes into 8 eight times and 2 goes into 8 four times. In both cases there is no remainder and the result should be 0. What am I misunderstanding?

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!

I’m on this same level right now @SenGru . I’d like to know the answer to this topic as well. :smile: :smile:
~Andrew

1 Like

Hey @SenGru Great question and welcome to the forum! I’m pretty sure the others have got it from here so I’m only going to remind you to give the solution to whoever has given you the best answer to your problem. It helps to keep the server organized and the system will automatically close the topic so that our mods do not have to

@SenGru it’s actually the other way.
Treat the % symbol like a division symbol.
So lets say we have 1 % 5. 1 / 5 is 0.2 if we divide. However, if we are working with integers and trying to find the remainder of 1 / 5, then that’s when we use modulo operators. So ideally you can imagine the modulo operator work like:
a % b = The remainder of a divided by b.
It seems like you’re treating it as b divided by a, but it’s actually the other way.

1 Like

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