[SOLVED] Pet Adjutant (Python)

The hero won’t say “heal” so the pet won’t go to the top x. The hero dies then since he doesn’t have enough health.

# Your pet can help you survive until you can escape.

def onHear(event):
    # event.message contains the text that was heard.
    # If somebody said "Fire":
    if event.message == "Fire":
        # Move to the bottom X mark with pet.moveXY()
        pet.moveXY(64, 16)
        # Say something with pet.say()
        pet.say("Fire!")
        pass
    # If somebody said "Heal":
    elif event.message == "Heal":
        # Move to the top X mark with pet.moveXY()
        pet.moveXY(63, 53)
        # Say something with pet.say()
        pet.say("Heal!")
        pass

pet.on("hear", onHear)

# You don't have to change the code below.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        pass
    else:
        # If your hero needs healing.
        if hero.health < hero.maxHealth / 2:
            hero.say("Heal")
        pass

This would be the troublesome code based on my analysis. This is a problem, because this is code that should not be altered.
However, it may be the fact that your hero only checks their health and if they are below the threshold if the enemy does not exist.
So, instead of your previous code, you would change it to the following.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        pass
    # If your hero needs healing.
    if hero.health < hero.maxHealth / 2:
        hero.say("Heal")

I hope this helps.

Also, remove the pass. It’s redundant and is not necessary to end an if statement, a change in indentation is enough.

I changed it to what you said and it still won’t work:

# Your pet can help you survive until you can escape.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        pass
    # If your hero needs healing.
    if hero.health < hero.maxHealth / 2:
        hero.say("Heal")

pet.on("hear", onHear)

# You don't have to change the code below.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        pass
    else:
        # If your hero needs healing.
        if hero.health < hero.maxHealth / 2:
            hero.say("Heal")
        pass

This code isn’t what I told you to change it to. It still uses an else block causing it to only check the hero’s health against the threshold when the enemy does not exist. Remove the else and lower the indent a level for all the code that was in the else block. Also, remove all the pass blocks because they don’t really do anything in my experience.

oops sorry it copied the old code

# Your pet can help you survive until you can escape.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        
    # If your hero needs healing.
    if hero.health < hero.maxHealth / 2:
        hero.say("Heal")

pet.on("hear", onHear)

# You don't have to change the code below.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        pass
    else:
        # If your hero needs healing.
        if hero.health < hero.maxHealth / 2:
            hero.say("Heal")
        pass

@fuery is this correct?

@A.Lee452 it’s still in an else attached to if enemy:.

# Your pet can help you survive until you can escape.

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        
    # If your hero needs healing.
    if hero.health < hero.maxHealth / 2:
        hero.say("Heal")

pet.on("hear", onHear)

# You don't have to change the code below.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # If an enemy is too strong.
        if enemy.type == "brawler":
            hero.say("Fire")
        else:
            hero.attack(enemy)
        pass
    # If your hero needs healing.
    if hero.health < hero.maxHealth / 2:
        hero.say("Heal")
        pass

@A.Lee452 this is correct. Let me know if it still doesn’t work

It still doesn’t work :frowning:

I’m not sure what it is then.

Ok thank you for trying

It was because my sword didn’t cause enough damage.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.