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;
}
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?