[SOLVED] Unexpected type error - code works anyway

In Spinach Power, I get a type error for enemy.health, but the code works anyway

  1. In real-world programming, is it important to eliminate the error if the program still works, or could I just leave it at that?
  2. If it is important, how can I solve this level without getting that error? The way I see it, after hero defeats the second enemy, the next hero.findNearestEnemy returns null, and the health of null can’t be determined. But I need that “health-check”, yeah?
1 Like

no, you do not need the health-check. hero.findNearestEnemy() returns the closest living enemy to the hero. but if the enemy is dead before the hero even reaches the enemy, then the hero would say ‘but it’s dead!’ and it would move on. To prevent that from happening, add a while loop which just checks if the enemy exists. The most that could happen is that right before you’re in range, something else kills it, but it most likely won’t say anything.

If you put a simple check

    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)

all problems in this case will be gone
another version of same code:

    while  enemy and enemy.health > 0:
        hero.attack(enemy)

@Seojin_Roy_Lee can you show the code of your idea?

3 Likes

That solves it. Duh :grimacing: should have figured that one out on my own, I guess. Thanks.

1 Like

…But there don’t seem to be any opinions on my first question…?

1 Like

Well if it’s real-world programming you obviously have to fix the error

Please keep posts on topic. Thanks.

1 Like

How is there anything off-topic here?

1 Like

Nevermind. @ MunkeyShynes edited Enderlord832’s post for being off topic

The topic of this thread was an error in the level, Spinach Power. Discussions about the appearance of someone’s Discourse profile are off-topic.

1 Like

@Archion, also please stop spamming likes.