# Benutze eine Schleife, die alle Gegner beinhaltet.
while enemies: #THIS IS THE LINE THAT'S NOT WORKING
enemy = enemies[enemyIndex]
# Wenn das Leben des Gegners kleiner als "leastHealth" ist,
if enemy.health < leastHealth:
# Mache diesen zu "weakest" und setzte "leastHealth" auf sein Leben.
weakest = enemy
leastHealth = enemy.health
# Don't forget to increase enemyIndex by 1.
enemyIndex += 1
if weakest:
# Greife den schwächsten Oger an.
hero.attack(weakest)
pass
That’s not a very helpful explanation. I’d like to understand that type error and what causes it in a), but not in b), when the line it referred to wasn’t changed…
I think I mayyy understand what you’re getting at…?
In plain language, the a) while enemies: should translate to “as long as their are enemies” (as long as the enemies array isn’t None), execute the following code. So I thought, the first loop should be skipped as soon as there aren’t any enemies left (hero.findEnemies returns None).
The reason why I thought this could work was that it also works with if: if also takes an object (I don’t know if that object can be an array though)!
if enemy:
#do the following things:...
(which IMO is stupid btw - short and handy, but illogical)
enemy is the result of hero.findNearestEnemy. If it returns None, then the if-statement evaluates to false and the ensuing code is skipped.
My reasoning was like this: isn’t while logically much the same as if? The only difference is that while keeps checking the condition repeatedly, if does it only once. So I thought while should accept the same arguments as if.
enemies is the result of hero.findEnemies.
Ergo, if hero.findEnemies returns an empty array, the while enemies: condition evaluates to false, and the ensuing code is skipped.
I’m gonna make another hypothesis: while and ifdo accept the same arguments, meaning both do accept objects, but only of type integer, float or string: it can’t be an array.
However, if this hypothesis is correct, then the type error should still definitely regard the line while enemies: (line 9), it clearly has nothing to do with line 12!
Conclusion: That error message is stupid. It doesn’t make sense. I’m at square one (while having reached further stages of a side quest). @xython, your answer is actually also referring to that side quest and isn’t dealing with this erroneous error message, I think?
Who can we ask whether or not my hypothesis is correct?