Prime Pathing Help Python

Did you test your isItPrime() function. Does it work?

Ah, it doesn’t work because n/2 can always divide into n to get n % n/2 == 0. This makes it think that everything is not prime and therefore doesn’t move.

1 Like

Maybe I could add for i in range(2, (n/2)-1):…

ok, now my code is

#Commented out to stop infinite loop.
# Get all your troops to the end of the path by passing over the mines.
# You can locate duds by finding the mines that have a value that is a prime number.
# Check the guide for clarification.

def isItPrime(value):
    for i in range(2, value-1):
        if value % i == 0:
            return False
        else:
            return True

traps = hero.findNearest(hero.findByType("fire-trap"))
for trap in traps:
    value = trap.value
    if isItPrime(value) == True:
        hero.say(value)
        hero.move(trap.pos)


I checked where the code wasn’t running, and it’s not working in the if isItPrime(value) == True: area. And I added a few more in the isItPrime function, and that’s not working either…I found that none of the if loops are running. I don’t see why,

1 Like

Think you want to have one fewer indent for the

        else:
            return True

ie the whole of the for loop needs to run before you get the result that the number is prime.

Jenny

You need to find isItPrime() function and to test it if it’s working.
Another way and maybe less computing hungry is to check your values against an array of prime numbers.