Grim Determonation Help please!

I’m stuck on grim determination. my paladins keep on blowing up while they are shielding…

# Your goal is to protect Reynaldo
# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.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):
    startpos = paladin.pos
    # 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()
    lowestFriend = friend
    if paladin.canCast("heal") and paladin.health < 200:
        hero.command(paladin, "cast", "heal", friend)
    if enemy:
        hero.command(paladin, "attack", enemy)
    else:
        hero.command(paladin, "move", startpos)
    pass
def commandPeasant(peasant):
    item = peasant.findNearestItem()
    pl = peasant
    if item:
        hero.command(pl, "move", item.pos)
def commandFriends():
    # Command your friends.
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            pass
        elif friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    if hero.gold>hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
    for friend in hero.findFriends():
        if friend.type == "griffin-rider":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x": 62, "y": 37})

This bit is wrong.
You’ve defined lowestFriend as friend (?). I’m not sure where friend has come from. Why not define lowestFriend using your nice function you wrote:
= lowestHealthPaladin()
Then, you should check if the lowestFriend’s health < 200, rather than the paladin’s health. Then make sure you command paladin to heal lowestFriend, not friend.
Danny

code:

# Your goal is to protect Reynaldo
# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.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):
    startpos = paladin.pos
    # 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()
    lowestFriend = lowestHealthPaladin()
    if paladin.canCast("heal") and lowestFriend.health < 200:
        hero.command(paladin, "cast", "heal", lowestFriend)
    if enemy:
        hero.command(paladin, "attack", enemy)
    else:
        hero.command(paladin, "move", startpos)
    pass
def commandPeasant(peasant):
    item = peasant.findNearestItem()
    pl = peasant
    if item:
        hero.command(pl, "move", item.pos)
def commandFriends():
    # Command your friends.
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            pass
        elif friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    if hero.gold>hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
    for friend in hero.findFriends():
        if friend.type == "griffin-rider":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x": 62, "y": 37})

error
image

1 Like

Ok, good job, you made the right changes and you’re almost there.
Two more changes required:

The reason you’re getting an error here is because you haven’t checked if lowestFriend actually exists. You’ve tried to access it’s health first.
Next:

If you have two if statements on after the other, and both if statements are true (there will always be enemies in this level), then the last if statement will run (if enemy), and the first one won’t.
To fix this use elif enemy. This means it will only run when there is an enemy, and the paladin can’t heal.
Danny

hi there I’m been coding since 6th grade and I’m still bad at it. Can you help me?

Sure, please could you make a new topic by clicking the + New Topic button on the main discourse page and pasting your code if from codecombat inbetween two lines of ```, that formats the code. Also please describe your issue.
Danny

code:

# Your goal is to protect Reynaldo
# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.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()
    lowestFriend = lowestHealthPaladin()
    if lowestFriend:
        if paladin.canCast("heal") and lowestFriend.health < 200:
            hero.command(paladin, "cast", "heal", lowestFriend)
        else:
            hero.command(lowestFriend, "shield")
    elif enemy:
        hero.command(paladin, "attack", enemy)
    pass
def commandPeasant(peasant):
    item = peasant.findNearestItem()
    pl = peasant
    if item:
        hero.command(pl, "move", item.pos)
def commandFriends():
    # Command your friends.
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            pass
        elif friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    if hero.gold>hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
    for friend in hero.findFriends():
        if friend.type == "griffin-rider":
            enemy = friend.findNearestEnemy()
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", {"x": 62, "y": 37})

Goals:
image
result:

or do I try again to submit or something

You’re almost there.
Think about what’s happening here. You are checking if lowestFriend exists, which is good. This means that the healing part won’t cause an error. You’ve also used elif enemy. However, you checked if lowestFriend exists separately from checking if the paladin can heal someone. You need to put if lowestFriend and if paladin.canCast(“heal”) and lowestFriend.health < 200: in the same if statement. Then elif enemy will actually work, because at the moment there will always be a lowestFriend, even if the paladins can’t cast heal on it.
Apart from that, everything’s right.
Danny

Thanks it worked! But i was going for the bonus.

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