i was trying to do this level but it keeps saying there’s an error with my code. Can anyone help me?
while True:
enemy = hero.findNearestEnemy()
# Check if there's an enemy and the enemy isn't a type "brawler", then attack.
if enemy.type == ("munchkin"):
if hero.isReady("cleave"):
hero.cleave()
# Else, move to the red X.
else:
hero.moveXY(40,20);
You want to cleave an enemy. The cleave method needs to have a target for you to cleave. Since you are using enemy, then cleave enemy. hero.cleave(enemy). Another thing is that cleave may not always be ready. With your current code, if cleave is not ready, then you would be a sitting duck until your cleave is ready once again. Even if cleave is not ready, you will still need to attack the enemies.
It isn’t strictly necessary to pass cleave a target. Without one, it will perform the action immediately around your character.
The current problem comes from if enemy.type == ("munchkin"). What if enemy is nothing (which CodeCombat reports as null)? There’s no property type on nothing! You’ll want to test for the existence of enemy first: if enemy and enemy.type == "munchkin":
I still have problems even when I recognize the enemy… I do not have enough strength to kill them all. The result is always incomplete.
while True:
enemy = hero.findNearestEnemy()
if enemy:
if not enemy.type is “munchkins”:
if hero.isReady(“cleave”):
hero.cleave(enemy)
else:
hero.attack(enemy)
pass
else:
hero.moveXY(40,20);