Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Medical Attention - Python

The only error is that my attack code gets null, even though enemy = hero.findNearestEnemy was directly above it. Heres my code.

# Ask the healer for help when you're under one-half health.

while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    # If your current health is less than the threshold,
    # move to the healing point and say, "heal me".
    # Otherwise, attack. You'll need to fight hard!
    if currentHealth < healingThreshold:
        hero.moveXY(65, 46)
        hero.say("Heal, please!")
    elif currentHealth > healingThreshold:
        enemy = hero.findNearestEnemy()
        hero.attack(enemy)

The hints are also completely useless.

hey @AlwaysConfused tip don’t write anything the soldiers will do it

You need an if statement before the attack statement. Your hero can only attack an enemy if there is an enemy.

um
if i do then i die.

Ok, but the error is there but my code is good. The error looks like this. I absolutely hate this error by the way.

You defined enemy (that’s good)
Then you need to check if there is an enemy
If there is an enemy, attack it.
Lydia

hmm weird I succeeded without writing any code

Well, that’s not really the way you’re supposed to do the level. You don’t really learn anything from that.

I do it all the time @Deadpool198 take advantage of the level

Ok cool, but HOW do i find it? enemy = hero.findNearestEnemy is the only way!

You already found the enemy, now you need to check if there is an enemy.
Lydia

like this:

if enemy:
    hero.attack(enemy)
    enemy = hero.findNearestEnemy()

but put it in a while true loop

You need find the enemy, which you have done, but you can only attack it if it exists, so you should do an if enemy:, and then have your attack statement inside of the if loop. The reason for if statements is that there is not always that something, in this case there is not always an enemy for your hero to attack.

It says there IS no enemy.

Already in one. (20)

Thanks i might try that

Apparently I won the game by doing absolutely nothing. I seemed to have healed a little, but my code broke somehow. Gotta fix that.

Code:

# Ask the healer for help when you're under one-half health.
enemy = hero.findNearestEnemy()
while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    # If your current health is less than the threshold,
    # move to the healing point and say, "heal me".
    # Otherwise, attack. You'll need to fight hard!
    if currentHealth < healingThreshold:
        hero.moveXY(65, 46)
        hero.say("Heal, please!")
    else:
        if enemy:
            hero.attack(enemy)

Hi AlwaysConfused,

At the moment you find an enemy at the start (outside the while True loop), but once you’ve killed that enemy you don’t find another enemy. What line of code do you need to move to do this?

Jenny

1 Like