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