Munchkin Harvest Level (Dunno how to solve it)


#1

Can anyone tell me how to solve this level? I use python here.

while True:
enemy = hero.findNearestEnemy()
if hero.isReady(“cleave”):
hero.cleave(enemy)
else:
hero.attack(enemy)

what is the problems here?Many thanks=)


#2
while True:
    enemy = hero.findNearestEnemy()
    if hero.isReady(“cleave”):
        hero.cleave(enemy)
    else:
        hero.attack(enemy)

If your code looks like this then there’s nothing wrong with it. Since you didn’t post it properly using the triple backticks (the “</>” button), then we can’t see your formatting and can’t tell if it’s correct or not.

This link tells all about posting code in this forum.

Edit:

Before your is.Ready if statement, what happens if there’s not an enemy? Is there always an enemy to cleave or attack?


#3


I dont understad whats that mean


#4

it means that the first time it iterated through the while True loop, your code is telling you to attack, but there is nothing to attack, thus it “got null” when looking for something to attack. It’s always best to check for enemies first with an “if enemy” statement. But the level starts out with you being surrounded with munchkins so now that I see your screen shot I’m wondering, are you wearing any type of glasses? If you don’t have any glasses, you won’t be able to see the enemy.


#5

You need to use

    else:
      if enemy:
          hero.attack(enemy)


#6

@Ironhead

The “if enemy” statement should go before the isReady statement. Enemy check should be first, before any of the attack code, including the cleave. But I don’t believe that’s the issue. Her code runs fine as written on my machine because the level starts out with the hero surrounded by munchkins so the if enemy statement shouldn’t be and isn’t required. If you look at the screenshot ChristyCat provided, it doesn’t look like she’s equipped with any glasses at all so she can’t see the munchkins whether they’re there or not.


#7

got it finally, thank you