Mad Maxer strikes back - type error


#1

In Mad Maxer strikes back:

a)

    # 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

why am I getting a type error here?

…but not with the following code (spoiler alert: solves this level):

b)

 while enemyIndex < len(enemies): #BUT THIS ONE IS WORKING
        enemy = enemies[enemyIndex]
    

…but the error is not about the line that I changed to make it work


#2

Sometimes the game knows it is wrong but it does not know where.


#3

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…


#4

Ok I realy don’t know.


#5

You have

    while enemies: #THIS IS THE LINE THAT'S NOT WORKING
        # someAction        
        enemyIndex += 1

vs

    while enemyIndex < len(enemies):
        # someAction        
        enemyIndex += 1

How do you exit from the first loop? The message is confusing but shows the the error.

The message must be something like IndexError.
http://www.pythontutor.com


[SOLVED] "TypeError: Tried to load an unsafe native value into the interpreter:object"
#6

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 if do 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?