My Code is Not Working in the Level Magic Exam [Solved]

Hello:
I am coding in Python, but my problem with this level is that my while True loop doesn’t repeat itself.
Can anybody tell me, what am I doing wrong?

Here is my code:

hero.moveXY(18,40)
while True:
    friendFoeObject()
    movement()
    

def friendFoeObject():
    while True:
        friend = hero.findNearestFriend()
        enemy =  hero.findNearestEnemy()
        item = hero.findNearestItem()
        if friend:
            if friend.type == "soldier":
                hero.cast("heal", friend)
            if friend.type == "goliath":
                hero.cast("grow", friend)
            if friend.type == "paladin":
                hero.cast("regen", friend)
        elif enemy:
            if enemy.type == "scout":
                hero.cast("force-bolt", enemy)
            if enemy.type == "brawler":
                hero.cast("shrink", enemy)
            if enemy.type == "ogre":
                hero.cast("poison-cloud", enemy)
        elif item:
            if item.type != "poison":
                hero.moveXY(item.pos.x, item.pos.y)
            else:
                continue
        else:
            continue

def movement():
    x = hero.pos.x
    y = hero.pos.y
    if y == 40:
        hero.moveXY(x,y-16)
    elif y == 24:
        hero.moveXY(x+16,y+16)

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

The code gets stuck in the first function because you have a while True: loop in it that doesn’t break. Remove that while True: loop and shift your indentation back on the rest of that section of code.

1 Like

Thank you for your help @brooksy125, that helped.
As you suggested I read the link you gave me.

If this is solved, please check the checkmark next to the comment that helped you the most!
Lydia

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