Munchkin Harvest Level (Dunno how to solve it)

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=)

1 Like
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.

https://discourse.codecombat.com/faq

Edit:

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


I dont understad whats that mean

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.

You need to use

    else:
      if enemy:
          hero.attack(enemy)

@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.

got it finally, thank you

It does not work!

[en_US.composer.my_button_text]

Hi @Shadow_Blay, welcome to the CodeCombat Discourse! :tada:
If you need help with your code, please post it onto the forum. This topic tells you how to format your code:

Once you’ve done that, could you also give some more information about the problem.
Thanks
Danny

I tried t use this but it dosen’t work:

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

Can you help

Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time! :partying_face: (I beat @enPointe77 to it again :D)

Your indentation error on line 4 breaks your whole code.
And if hero.isReady("cleave"): should be if enemy and hero.isReady("cleave"), since otherwise, you will cleave even if there aren’t any enemies.

2 Likes