[SOLVED] Unexpected type error - code works anyway


#1

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?

#2

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.


#3

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?


#4

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


#5

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


#6

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


#7

Please keep posts on topic. Thanks.


#8

How is there anything off-topic here?


#9

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


#10

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.


#11

@Enderlord832, also please stop spamming likes.