Medical Attention trouble

Hey I’m stuck on this level. Any help? Here’s my code:

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 me”)
else:
enemy = hero.findNearestEnemy()
hero.attack(enemy)

I’m not quite sure how to put it in code format sorry

Please learn how to post your code properly. There are many people here that are willing and able to help but you need to post your code properly so that we can see the structure. Help us help you. Thanks!
Button01

Alright. Here it is:

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 me”)
else:
enemy = hero.findNearestEnemy()
hero.attack(enemy)

Here is an updated version:

while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    enemy = hero.findNearestEnemy()
    # 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 me")
    else:
        hero.attack(enemy)
            

1 Like

I’ll bet you’re getting a null error, correct? You should get in the habit of always checking to see if there’s an enemy before attacking. Otherwise, when there’s no enemy to satisfy the condition, the code fails.

2 Likes