Fizz buzz help with modulo


#1
# 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?


#2

What is step 15?

Try

if steps % 3 and % 5 == 0:

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


#3

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


#4

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


#5

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


#6

Ok sorry i’m just trying to help


#7

Maybe @Deadpool198 or @MunkeyShynes can help.


#8

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:

#9

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:


#10

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…


#11

thanks :slight_smile:


#12

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)

#13

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