Kelintaph Crusader Infinite Loop

I have tried all I can to fix the infinite loop; I’ve condensed, revised, and even commented out sections, but it still stops loading past 20-ish seconds. Here is my code so far:

Pos=[{"x":42,"y":15},{"x":78,"y":14},{"x":68,"y":16}]
def distanceTo(unit,target):
    distance=None
    if unit.type=="witch":
        continue
    elif unit.health>0:
        sx=unit.pos.x-target.x
        sy=unit.pos.y-target.y
        distance=Math.pow(sx, 2)+Math.pow(sy,2)
    return distance
#def nearestarcher(unit):
#    ld=9999
#    neararcher= None
#    for target in hero.findFriends:
#        dtarget=distanceTo(unit,target)
#        if target.type=="archer":
#            if dtarget<ld:
#                ld=dtarget
#                neararcher=target
#                continue
#            else: 
#                continue
#    return neararcher
def findMostHealth(enemies):
    target = None
    targetHealth = 0
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > targetHealth and enemy.type!="brawler" and enemy.type!="catapult":
            target = enemy
            targetHealth = enemy.health
        enemyIndex += 1
    return target
def findWitch(enemies):
    for enemy in enemies:
        if enemy.type=="witch" and enemy.type!="ogre" and enemy.type!="skeleton":
            return enemy
c=0
i=0  
pos=[{"x":31,"y":58},{"x":50,"y":54},{"x":53,"y":38},{"x":78,"y":40}]
def mF():
    hero.findNearest(hero.findFriends())
    for friend in hero.findFriends():
        v=0
        for i in range(4):
            tpos=pos[v]
            while distanceTo(friend, tpos)>2:
                hero.command(friend, "move", tpos)
            v+=1
def cF():
    for friend in hero.findFriends():
        #this section calls up findWitch function+calls up lowesthealth function for healing use by paladin 
        lowest=lowesthealth()
        enemy=friend.findEnemies()
        if len(enemy)>0:
            witch=findWitch(enemy)
            tankiest=findMostHealth(enemy)
        else:
            break
        if friend.type=="archer":         
            if witch:
                hero.command(friend, "attack", "Yzzrith")
            elif tankiest:
                hero.command(friend, "attack", tankiest)
        elif friend.type=="paladin":
            if friend.canCast("heal"):
                hero.command(friend, "cast", "heal", lowest)
            if "Yzzrith" and i==0:
                hero.command(friend,"move", {"x": 22,"y": 52})
                i+=1
                if friend.canCast("heal"):
                    hero.command(friend, "cast", "heal",friend)
            elif "Yzzrith" and "Yzzrith".health>0 and i>0:    
                hero.command(friend, "shield")
            else:
                hero.command(friend, "attack", tankiest)
        elif friend.type=="soldier" and c==0:
            hero.command(friend, "defend",{"x": 12,"y":45 })
            c+=1
        elif friend.type=="soldier" and c==1:
            hero.command(friend, "defend", {"x":15,"y":40})
loops=-1
def mh():
    loops+=1
    if loops==0:
        hero.cast("chain-lightning", "Trogdor")
    cF()
    hero.jumpTo(Pos[0])
    hero.cast("time-travel", hero)
    cF()
    while distanceTo(hero,Pos[2])>2:
        hero.move(Pos[2])
        cF()
    for i in range(3):
        if hero.findNearestEnemy():
            for i in range(2):
                hero.attack(hero.findNearestEnemy())
    hero.shield(6)
    hero.moveXY(78, 14)
    loops+=1
def lowesthealth():
    lowest=None
    lowesthealth=9999
    friends = self.findFriends()
    for friend in friends: 
        if friend.health>0:
            health=friend.health/friend.maxHealth
            if health<lowesthealth:
                health=lowesthealth
                lowest=friend
    return lowest
while True:
    mh()
    if hero.time>=15:
        mF()

Here is an issue I found… Is this part supposed to be comments?

#def nearestarcher(unit):
#    ld=9999
#    neararcher= None
#    for target in hero.findFriends:
#        dtarget=distanceTo(unit,target)
#        if target.type=="archer":
#            if dtarget<ld:
#                ld=dtarget
#                neararcher=target
#                continue
#            else: 
#                continue
#    return neararcher

Yes, I commented out this unnecessary part.

Also what hero, equipment, and language are you using? And where is the error? (post a screenshot of the error, including line #)

Screenshot 2023-03-02 9.12.45 AM

I am using Alejandro the Duelist with my coding language set to Python.


The error is the dreaded infinite loop.

I have a method that works, but the code never finishes running… D:<

Sorry to ask, but do you have a link to the level?

here is the link: CodeCombat - Coding games to learn Python and JavaScript?

Welcome, penguiny5! (20 chars)

thank you!(20 chars)

nvm worng level (20 chars)