I’m new to CodeCombat. Decided to try and learn a new skill for New Years and this is my first attempt at programming. I’m stuck trying to figure out why my hero isn’t saying a total when hero moves to the X by the mage. Here is my code:
while True:
traps = hero.findHazards("fire-traps")
lowestTrap = 0
trapIndex = 0
while trapIndex < len(traps):
trap = traps[trapIndex]
trapIndex += 1
if trap.value > lowestTrap:
lowestTrap = trap.value
traps = hero.findHazards("fire-traps")
highestTrap = 9999
trapIndex = 0
while trapIndex < len(traps):
trap = traps[trapIndex]
trapIndex += 1
if trap.value < highestTrap:
highestTrap = trap.value
hero.moveXY(27, 42)
hero.say(lowestTrap.value + highestTrap.value)
break
What am I missing? Also, I put trapIndex += 1 in this code because I’ve been using enemyIndex +=1 or friendIndex +=1 on previous levels, but I’m not really sure what it does, just that it needs to be there for the code to work. Thank,
Twisted
Try getting rid of the trap total, use trapIndex += 1, your trying to find a value of something that i’m pretty sure doesn’t have values, it’s so much easier to just increment the index by 1 everytime you come across a trap.
On this level there are 6 traps showing values. The objective is to add the lowest value with the highest value and tell it to the mage.
I figured out what I was doing wrong. Here is a snipped of my new code:
while True:
traps = hero.findHazards("fire-traps")
lowestTrap = None
lowestValue = 0
trapIndex = 0
while trapIndex < len(traps):
trap = traps[trapIndex]
trapIndex += 1
if trap.value > lowestValue:
lowestTrap = trap
lowestValue = trap.value
Nice, did you get it fixed? Looks fine now i would think. A bit cleaner as well.
do I have to play this level in order to continue with the campaign?
Because Sarven Sum is a subscriber level, it’s probably optional.
The code enemyIndex +=1 is just shorthand for saying
enemyIndex = enemyIndex + 1
It means, set the new variable value to be equal to the old variable value plus one.
It is how we increment the index, so that each time through the loop the index will hold a different value, stepping by one.