Magic Exam: how to pass

Hi! I’m having trouble with “Magic exam” in “Backwoods Forest”.
My problem is that I am only reaching 17 gold, and I would like to at least reach 18, hopefully 24.
If anyone out there has gotten this level with 18 or 24 gold, can you explain how you did it?
I’d like to know if you think there’s anything wrong with code, too.
Here it is:

# Try to get the best grade (gold) at the magic exam.
# Move to each X mark, then use a spell.
# There are 3 friends, 3 enemies, and 2 items.

def castIfReady(spellName , target):
    ready = hero.canCast(spellName, target)
    if ready:
        hero.cast(spellName, target)

def enemyFriendItem():
    friend = hero.findNearestFriend()
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    if enemy:
        if enemy.type == "ogre":
            castIfReady("force-bolt", enemy)
            hero.say("Force Bolt.")
        if enemy.type == "munchkin":
            castIfReady("shrink", enemy)
            hero.say("Shrink!")
        if enemy.type == "scout":
            castIfReady("poison-cloud", enemy)
            hero.say("Poison!")
        if enemy.type == "brawler":
            castIfReady("poison-cloud", enemy)
            hero.say("Poison yee-haw!")
    if friend:
        castIfReady("heal", friend)
        hero.say("Heal ye!")
    if item:
        if item.type == "potion":
            x = item.pos.x
            y = item.pos.y 
            hero.moveXY(x, y)
            hero.say("Take the item!")
        else:
            hero.say("Don't take it!")

while True:
    friend = hero.findNearestFriend()
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    hero.moveXY(18, 40)
    enemyFriendItem()
    hero.moveXY(18, 24)
    enemyFriendItem()
    hero.moveXY(34, 40)
    enemyFriendItem()
    hero.moveXY(34, 24)
    enemyFriendItem()
    hero.moveXY(50, 40)
    enemyFriendItem()
    hero.moveXY(50, 24)
    enemyFriendItem()
    hero.moveXY(66, 40)
    enemyFriendItem()
    hero.moveXY(66, 24)
    enemyFriendItem()

Hello! First of all, you should make only one function that checks all friends and enemies and then cast the right spell for them. Take your time for this

Ok, thanks, let me try that.

No he/she is doing the right thing. What goes wrong.

Well, I will leave it up to you then I guess @milton.jinich

I tried your advise @JoPro_8000, however, I did not get any gold this time. I’m going to undo it, but I appreciate the gesture.

Thanks, as said, I’m kinda busy right now so I will leave it up to @milton.jinich to help you

Hooray! I’ve gotten 20 gold on my own. Topic closed.

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