Summit's Gate - Bug in code or bug in level?

When I run this code on Summit’s Gate, the game stops after I place a purple flag (but there’s no infinite loop error). There’s either a bug in my code or a bug in the level, but I’m not sure which. Here’s my code:

lev = 0
def lowestHealth():
    weakness = 75
    weakest = None
    for friend in friends:
        if friend.team != "ogres" and friend.maxHealth - friend.health > weakness:
            weakness = friend.maxHealth - friend.health
            weakest = friend
    return weakest
    
def commandHero(level):
    if hero.findFlag("green"):
        hero.pickUpFlag(hero.findFlag())
    elif hero.findFlag("black"):
        if hero.canCast("invisibility"):
            hero.cast("invisibility", hero)
        hero.pickUpFlag(hero.findFlag())
    elif hero.findFlag("violet"):
        hero.removeFlag(hero.findFlag())
        door = hero.findNearest(hero.findByType("door"))
        while door.health > 0:
            friends = hero.findFriends()
            for friend in friends:
                hero.command(friend, "attack", door)
            hero.attack(door)
        level += 1
    elif hero.findByType("catapult")[0]:
        hero.attack(hero.findNearest(hero.findByType("catapult")))
    elif hero.findByType("warlock")[0]:
        hero.attack(hero.findNearest(hero.findByType("warlock")))
    elif len(enemies) and level != 1:
        target = None
        minDist = 9999
        for enemy in enemies:
            if hero.distanceTo(enemy) < minDist and enemy.type != "door" and enemy.type != "tower":
                    minDist = hero.distanceTo(enemy)
                    target = enemy
        if target:
            hero.attack(target)
        else:
            hero.say(level)
    elif len(enemies):
        hero.attack(hero.findNearest(enemies))
    return level

def commandFriends(level):
    if level == 0:
        for friend in friends:
            if friend.type == "paladin":
                if friend.canCast("heal") and lowestHealth():
                    hero.command(friend, "cast", "heal", lowestHealth())
                elif friend.canCast("heal", hero) and hero.health/hero.maxHealth < .95:
                    hero.command(friend, "cast", "heal", hero)
                elif len(enemies):
                    target = None
                    minDist = 9999
                    for enemy in enemies:
                        if friend.distanceTo(enemy) < minDist and enemy.type != "door" and enemy.type != "tower":
                            minDist = friend.distanceTo(enemy)
                            target = enemy
                    if target:
                        hero.command(friend, "attack", target)
            elif len(enemies):
                target = None
                minDist = 9999
                for enemy in enemies:
                    if friend.distanceTo(enemy) < minDist and enemy.type != "door" and enemy.type != "tower":
                        minDist = friend.distanceTo(enemy)
                        target = enemy
                if target:
                    hero.command(friend, "attack", target)
    elif level == 1:
        enemy = hero.findNearest(enemies)
        if enemy:
            hero.attack(enemy)

while lev == 0:
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    commandFriends(lev)
    lev = commandHero(lev)

for friend in friends:
    hero.command(friend, "move", {"x": 90, "y": 35})

while lev == 1:
    commandHero(lev)

More info:
Browser - chrome
OS - windows
The bug only happens after dropping a violet flag.

I found the bug. Turns out I was forgetting to redefine enemies in the second loop.