Fizz buzz help with modulo

# Be aware of hidden traps, and count your steps
# Coins are separated by 10 meters distance

steps = 1

while True:
    # If the number of steps is divisible by 3 AND by 5 -- move to North-West
    if steps % 3 and 5 == 0:
            hero.moveXY(hero.pos.x - 10, hero.pos.y + 10)
    # Else if the number of steps is divisible by 3 -- move to East
    else:
        if steps % 3 == 0:
            hero.moveXY(hero.pos.x + 10, hero.pos.y)
    # Else if the number of steps is divisible by 5 -- move to West
        else:
            if steps % 5 == 0:
                hero.moveXY(hero.pos.x - 10, hero.pos.y)
            else:
                hero.moveXY(hero.pos.x, hero.pos.y + 10)
    
    steps += 1


Thought I’d got this one, but seem to be getting blown up on step 15?

What is step 15?

Try

if steps % 3 and % 5 == 0:

I don’t know if thats the problem but you can try it.

No this does not help - in fact this yields an instant code error sorry.

Oh well sorry I don’t code python :dizzy_face: :hammer:, but what is is step 15?

Then why are you responding on a python forum…?!? step refers simply to how many steps the hero has taken.

Ok sorry i’m just trying to help

1 Like

Maybe @Deadpool198 or @MunkeyShynes can help.

This is on the right track but not complete. Think about what that statement is really saying. Remember that computers are very literal and kinda stupid. They lack the ability to figure out what you mean. You have to be very precise. It should be:

if steps % 3 == 0 and steps % 5 == 0:
1 Like

I always wish there was a function which let you do that, like:

if enemy.type == "ogre" or "munchkin" or "thrower":
    hero.attack(enemy)
#but it has to make it twice as frickin long. (I run into this a lot, especially with a findEnemyHero function)
if enemy.type == "ogre" or enemy.type == "munchkin" or enemy.type == "thrower":

Anyway, just wanted to share my thoughts.
:lion:

It won’t work with current python logic though. It’s the or's that are separating them so it’s like if enemy.type==‘ogre-m’ or ‘munchkin’. The code won’t know what munchkin is unless you assign enemy.type to each one. I think…

thanks :slight_smile:

How to use *args and **kwargs in Python
I got your idea and this is my function. It’s not well tested and I will be glad if someone improves it:

def orTypes(unit, *types):
    for type in types:
        if unit and unit.type == type:
            return type
    return False
# possible use
enemy = hero.findNearestEnemy()
if enemy and enemy.health > 0 and orTypes(enemy,'witch', 'thrower'):
    hero.attack(enemy)
3 Likes

I really like that idea! Thanks, I’ll put it in my code.
:lion:

same code in java-script - also not tested
The arguments object

function orTypes ( unit){
  var returnValue = false;
  for (var i = 1; i < arguments.length; i++ ){
      // unit is arguments[0]
      var type = arguments[i];
      if (unit && unit.type == type){
        returnValue = true;
        // you can also assign unit or type to returnValue
      }  
  }
  return returnValue;
}
// possible use
var enemy = hero.findNearestEnemy();
if (enemy && enemy.health > 0 && orTypes(enemy,'witch', 'thrower') 
    hero.attack(enemy);