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

Having Trouble with "Medical Attention" [Python]

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

Here is my code:

while True:
    currentHealth =
    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")

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:

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

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.