Grim determination: paladins not healing

I am having trouble with this level. The problem is that the paladins can’t heal. I don’t know what it is because I can’t find an answer on the discourse. I have tried on different computers and it still doesn’t work.

# Your goal is to protect Reynaldo

# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = self.findFriends()
    for friend in friends:
        if friend.type != "paladin":
            continue
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def commandPaladin(paladin):
    # Heal the paladin with the lowest health using lowestHealthPaladin()
    # You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    # Paladins can also shield: command(paladin, "shield")
    # And don't forget, they can attack, too!
    
    enemy = paladin.findNearestEnemy()
    
    if paladin.canCast('heal'):
        self.command(paladin, "cast", "heal", lowestHealthPaladin())
    elif enemy:
        self.command(paladin, "attack", enemy)

def commandFriends():
    # Command your friends.
    friends = self.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            #commandPeasant(friend)
            coin = friend.findNearestItem()
            if coin:
                self.command(friend, "move", coin.pos)
        elif friend.type == "griffin-rider":
            #commandGriffin(friend)
            enemy = friend.findNearestEnemy()
            if enemy:
                self.command(friend, "attack", enemy)
        elif friend.type == "paladin":
            commandPaladin(friend)

loop:
    commandFriends()
    # Summon griffin riders!
    if self.gold > self.costOf('griffin-rider'):
        self.summon('griffin-rider')



    return lowestFriend

Does it say anything about your code? Like “fix your code” or “error on line 2” or something?

What is the

return lowestFriend

at the end of you code? Can’t test now but looks like will automatically exit your main function and terminate all your code after the first iteration

No, it does not show an error, but the red x under the hero is reporting one.

I don’t know why that line of code is there. Unfortunately, it doesn’t work without it.

Of course it gives an error. Remove the last line of code, then run the code:

  1. You will get a red X under your hero. Scroll through your code on the left until you see the red market line with the error.

  2. It is:

if paladin.canCast('heal'):
        self.command(paladin, "cast", "heal", lowestHealthPaladin())

and the error is lowestHealthPaladin() is null, which can happen if no-one is injured.

  1. Remember how you fixed the null enemy issue:
self.attack(self.findNearestEnemy())

will fall but

enemy=self.findNearestEnemy()
if enemy:
    self.attack(enemy)

will not fall

1 Like

Hmm, not sure about that. I’ll invite @nick