# This function allows to fight until the certain time
# and report about defeated enemies.
def fightAndReport(untilTime):
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
enemy.health <= 0
defeated += 1
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(defeated)
# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)
# Collect coins until the clock reaches 30 seconds.
def collectAndReport(untilTime):
coin = hero.findNearestItem()
collected = 0
while True:
coin = hero.findNearestItem()
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(hero.gold)
collectAndReport(30)
# Tell Naria how much gold you collected.
hero.say(hero.gold)
# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)```
for some reason when im done with the first round of enemies, my character always says 24 instead of 6 for enemies defeated. ive changed the value to defeated += .25 and that will give me 6 but then give me the wrong answer on the last round of defeating enemies. Im ready to pull my hair out because Im pretty sure this is the correct code.
on an unrelated note:
as much as I love code combat and im learning a lot, this program seems pretty buggy all the time, like I will get success when I run my code (in other levels) and then upon submission it says that I failed, if I submit 2 or 3 more times then the code gets accepted and I pass.
should be
if enemy.health <= 0:
thank you for the pointer but I think there is still something else missing.
I got this error when I tried that.
You need to indent that more
Actually, the indent is fineâŚwhat happens if there is no enemy?
Youâve stacked multiple âifâ statements, which means they are each independent of the others. Instead, you should combine them in to fewer statements, or else nest them.
I think what @Gamespoiler means is that @Sage_Castillo needs to indent
if enemy.health <= 0
more so that the hero only checks the enemyâs health if there is an enemy.
yes that is what I meant
AhaâŚthat would be nesting them then. I didnât realize that is what you meant.
def fightAndReport(untilTime):
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
if enemy.health <= 0:
defeated += 1
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(defeated)
# Fight 15 seconds and tell Naria how many enemies you defeated.
fightAndReport(15)
# Collect coins until the clock reaches 30 seconds.
def collectAndReport(untilTime):
coin = hero.findNearestItem()
collected = 0
while True:
coin = hero.findNearestItem()
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(hero.gold)
collectAndReport(30)
# Tell Naria how much gold you collected.
hero.say(hero.gold)
# Fight enemies until the clock reaches 45 seconds.
fightAndReport(45)```
I used it like this and Iâm still getting an error. The issue is that my character is saying 24 when iâve only slain 6 enemy.
this same error was happening in double Cheek where I had to change the value associated with
defeated += 1 to defeated += .5 in order to get my character to kill all the ogres and then go look for coins. the coins wouldnât even generate until all the ogres were killed so itâd just be stuck.
once I changed the value to .5 the level worked like it should. THAT same thing is happening to me on book keeper. if I change the value to .25 then my character will say the correct number of defeated enemy. The other problem is that if I keep this .25 value for defeated += .25 then I wonât get the correct number for the 2nd round of defeating enemies. defeated += .25 yields me 4.75 instead of 5.
this leads me to believe the level is buggy. the whole function on the top part was already written by Codecombat so I donât see how there could be a problem with their code.
When I run your code, it works just fine for me. @dedreous @Deadpool198 can you guys figure out what is wrong?
these kinds of bugs make learning very frustrating. Half the time I feel like im really making progress and the other Half I feel like an idiot because I canât figure it out, only to realize that it was a bug anyways.
I would hesitate calling it a bug, since it works for others. If it truly was a bug, you would expect it to fail all the time. I am also able to run your code with no issues. Have you tried using a different browser?
Means that the code youâve written more closely fits a specific seedâŚeach time you submit, you get a new seed, until you finally land on one that is a better match.
I think youâve got things backwards. if it failed everytime then you could say its bad code but the fact that it works for other people and not me says something. Like obviously my code must be good if other people can pass without editing it.
I appreciate everybody replying so fast, im just not any closer to understanding why this isnât working.
So then, the task is to figure out whatâs different between you and themâŚsuch as your browser, perhaps?
why would using another browser make a difference? it just doesnât make sense, I also donât want to have to install another browser just to get through this level. Im using safari if that makes a difference.
I just want to understand your reasoning other than its just regular olâ troubleshooting.
Hi, again, as with the others, it works for me. So I think we should try and work out all the differences between your level environment and mine.
Please could I have: a screenshot of your equipment and of the level screen when you say your amount of gold. Then some more info about any previous problems etc. Also please could I have a screenshot of your code in the level if you can fit it all in, or in two if not. (That may seem weird but it takes out any possibility of errors in transmission between you and me).
Sorry about having to do all this, but you need a lot of info to find a bug.
Danny
P.S. I think the browser matters a bit more with loading related issues, but if nothing else works you might want to give it a try-- you never know!
This is funny
The code is OK, but not for your hero and especially for lightning twig staff.
See carefully the stats:
To kill a scout you need 50/13.36 = 3.74 attacks. You cannot interrupt an attack so youâll need 4 attacks.
The piece of code will be:
defeated = 0
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
hero.attack(enemy)
hero.attack(enemy)
hero.attack(enemy)
defeated += 1;
if hero.time > untilTime:
break
hero.moveXY(59, 33)
hero.say(defeated)
You can make it also in a different way, but the reasoning itâs a little bit more obscure:
if enemy:
startTime = hero.time
while hero.time - startTime < 1:
hero.attack(enemy)
defeated += 1;
youâll see many â⌠but itâs dead!â mesages
that Iâm hating so much!
The easiest way is to stick to a simple free hero

P.S.
I think the best way to do the above trick is to initialize the weaponâs damage.
twigDamage = 13.36
enemy = hero.findNearestEnemy()
if enemy:
n = 0
while enemy.maxHealth - n*twigDamage > 0:
hero.attack(enemy)
n +=1
defeated += 1;
Thank you for that lovely solution @xython . I especially like the n*twigDamage, very clever.