Having Trouble with "Medical Attention" [Python]

Whenever I try to put in “hero.findNearestEnemy” it never works.

Here is my code:

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.powerUp()
        hero.attack(enemy)
    

Can anyone help me?

You should get in the habit of always checking for an enemy before attacking, otherwise if there is not an enemy when the code iterates through the attack section, it will return a null error. You’ve already defined enemy as hero.findNearestEnemy() so there’s no need to use this method again. Try this:

else:
    if enemy:  # <---  More often than not this is a good idea before attacking
        hero.powerUp()
        hero.attack(enemy)
else:
    if enemy:
        if hero.isReady("powerup"):
            hero.powerUp()
        else:
            hero.attack(enemy)

That works better, since @MunkeyShynes’s code would force the hero to powerUp even if it is not ready. (which means the hero can’t do anything until it powerUps again)

1 Like

btw try getting the sword of the temple guard if you have enough gems. It will really help with the levels. Just a suggestion.

Oh I only have 412 gems. Should I just save up for better armor then?

save up for better sword first, then better armor. (actually, get the dragon chestplate first, then the sword, then the helm)

That’l take me like 20 levels each, but ok.

@Alphaspectre You could mine for gems by running simulations.

I’m having trouble. My hero won’t attack. The report says: ArgumentError: Attack's argument target should have type object, but got null. Is there always a target to attack? Use if. Here’s my code:

while True:
    currentHealth = hero.health
    healingThreshold = hero.maxHealth / 2
    enemy = hero.findNearestEnemy()
    if currentHealth == healingThreshold:
        hero.moveXY(65, 46)
        hero.say("Heal me, please!")
    else:
        hero.attack(enemy)

Welcome to the forum @Upbeatsans.
The problem is here:

else:
    hero.attack(enemy)

You are commanding your hero to attack the enemy, but what happens if there is no enemy for your hero to attack? You should use an if statement for your hero to attack if there is an enemy.