# 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.
while True:
traps = hero.findByType("fire-trap")
trapsIndex = 0
for trap in traps:
trap = trap[trapsIndex]
trap.value = value
for i in range(2, Math.ceil(Math.sqrt(value))):
if n % i == 0:
hero.move(trap.pos)
else:
trapsIndex += 1
I get an error message that I can’t write a property of a non-object type which is value. It’s on the line trap.value = value. Any help someone can give me?
You have to get your method straight.
Are you using for trap in traps or traps[trapsIndex]. At the moment you’ve got for trap in traps, which defines trap, but for some reason you’re also defining trap as trap[trapsIndex]. That’s incorrect because trap isn’t an array, that’s traps.
Also what is n? have you defined it?
Danny
# 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.
while True:
traps = hero.findByType("fire-trap")
trapsIndex = 0
for trap in traps:
value = trap.value
for i in range(2, Math.ceil(Math.sqrt(value))):
if value % i == 0:
hero.moveXY(hero.pos.x, trap.pos.y)
hero.move(trap.pos)
else:
continue
Well now I’ve changed it a little more and now have
# 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.
while True:
traps = hero.findByType("fire-trap")
for trap in traps:
value = trap.value
for i in range(2, Math.ceil(Math.sqrt(value))):
if value % i == 0:
hero.moveXY(trap.pos.x + 10, trap.pos.y)
hero.move(trap.pos)
else:
i += 1
This is quite a hard level - it took me a lot of attempts to get through it. A couple of things to get you started:
At the moment you’re finding all of the fire-traps (or at least all the ones in range of your glasses). Is that what you want?
You’re on the right lines for how to find a prime number. But think carefully about what “if value % i == 0” actually means. Is value a prime number if this is true?
I made the ‘check if a number is prime’ into a function, so it could all run before my hero took any action.
Try getting these things sorted so your hero can get through the first set of fire-traps. Once you have the basics working there will then be some more problems to solve.
# 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(number):
for i in range(2, Math.ceil(Math.sqrt(number))):
if number % i != 0:
return True
else:
i += 1
while True:
traps = hero.findNearest(hero.findByType("fire-trap"))
for trap in traps:
value = trap.value
isItPrime(value)
if isItPrime(value) == True:
hero.move(trap.pos)
else:
pass
I don’t see what you mean by finding all of the fire-traps. Would I have to see the ones that closest, like in the code up there ^^^ with hero.findNearest(hero.findByType("fire-trap")) AND I have a infinite loop. I know that I have to command the troops, but I don’t want to worry about that yet.
Have another look at your isItPrime function. You’re thinking on the right lines, but what happens if you put 15 into the function? i starts at 2; and 15 % 2 = 1, which doesn’t equal 0; so the function returns True. Is that right?
I don’t think you want the i += 1 in there as the for loop will increase i each time - I suspect this is what is causing the errors.
In terms of finding the closest fire-traps, you could just restrict them by distance…
Keep going with it - you sound as though you’ve got a structure and you’re working through each problem as you come to it (which seem to be the key to programming).
#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(number):
for i in range(2, number):
if number % i != 0:
return True
while True:
traps = hero.findNearest(hero.findByType("fire-trap"))
for trap in traps:
if trap:
value = trap.value
isItPrime(value)
if isItPrime(value) == True:
hero.move(trap.pos)
else:
continue
hmmmm, now it says that in trap.pos, trap is not defined
numbs = [77, 3, 11, 21, 42, 55, 51]
def isItPrime(number):
for i in range(2, number):
if number % i != 0:
print( str(i) + " " + str(number) + " " + str(number % i))
return True
for num in numbs:
if isItPrime(num):
print(num)
# shorturl.at/mxHL4 shortcut from http://www.pythontutor.com
So you must write a better isItPrime() function or find one in internet ( and there is nothing wrong doing it
Shotrcut where the code can be run http://shorturl.at/mxHL4
P.S. For years I check snippets of codecombat code in http://www.pythontutor.com
Ok. I found a different one on the web .But, now it’s the moving part. I don’t move anywhere. Here I’ll show it again.
Code
#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(n):
if n <= 1 or n % 1 > 0:
isPrime = False
for i in range(2, n/2):
if n % i == 0:
isPrime = False
return False
else:
isPrime = True
return True
while True:
traps = hero.findNearest(hero.findByType("fire-trap"))
for trap in traps:
if trap:
value = trap.value
isItPrime(value)
if isItPrime(value) == True:
hero.move(trap.pos)
else:
continue