Forest Shadow Bug or just me?

I did my code correctly but my hero keeps targeting an ogre instead of a thrower or a munchkin.
This is my code:

// Attack only the small ogres in the forest.
// Collect coins and gems only.
// Don’t leave the forest and don’t eat/drink anything.


`var enemy = hero.findNearestEnemy();`


var item = hero.findNearestItem();

while(true) {
    // Find the nearest enemy.
    // Attack it only if its type is "thrower" or "munchkin".
    if (enemy.type == "thrower" || "munchkin") {
      hero.attack(enemy);
}
}
// Find the nearest item.
// Collect it only if its type is "gem" or "coin".
if (item.type == "gem" || "coin") {
    var pos = item.pos;
    var x = pos.x;
    var y = pos.y;
}

Put these lines inside while-loop:

var enemy = hero.findNearestEnemy();
var item = hero.findNearestItem();

I’m going to do it. Doing codecombat while checking on this discourse

It just ran out of time

There is an requirement with the loops where you need to do something each iteration or it causes an issue.

So the statement:

if (enemy.type == "thrower" || "munchkin") {
  hero.attack(enemy);
}

Should have an else clause that does something like `hero.say(“No enemies available”);

[please, don’t post solutions]

There’s really a bug in the level.

And the way to reveal it is to use

if enemy.type == “thrower” or “munchkin”:

But right before attacking, have the Hero say the enemy type, like so:

    hero.say(enemy.type)
    hero.attack(enemy)

Now the Hero says “Ogre”, and attacks the ogre.

The Python is disobedient, but there’s a way around it :wink:

Instead of using

if enemy.type == “thrower” or “munchkin”:

Use

if enemy.type != “ogre”:

(If enemy type is not equal to Ogre, then attack)

Issue fixed.

I’ll agree that it definitely does not like to check the combination of types in this arrangement. I can’t say that I’ve ever seen an “or” statement shortened like that in Python.

I made it work by checking for both separately.

if enemy.type == "thrower" or  enemy.type == "munchkin":

But now I’m curious, is there a more condensed way to check these with an “or” statement?

1 Like

Fizz buzz help with modulo?

Nicely done! :clap: Have you been able to test it much? I’ll have to get familiar with the *args and **kwargs usage. That looks handy.

No, didn’t test it. Posted the same function written in java-script in the old topic.