Medical Attention trouble


#1

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)


#2

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


#3

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


#4

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)

#5

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)
            


#6

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.