Summits gate-infinite loop error

It crashed when I am killing the chieftain.
Here is my code:

# Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
def gate1():
    hero.moveXY(62, 33)
    for i in range(8):
        catapult = pet.findNearestByType("catapult")
        if catapult:
            hero.attack(catapult)
    for friend in hero.findFriends():
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
while hero.time < 30:
    for friend in hero.findFriends():
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
    gate1()
for i in range(4):
    hero.summon("archer")
while hero.time < 32:
    hero.attack(hero.findNearestEnemy())
    for friend in hero.findFriends():
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)

hero.moveXY(128, 36)
while hero.time < 69:
    hero.shield()
    for friend in hero.findFriends():
        if friend.type == "paladin":
            if (friend.canCast("heal")):
                hero.command(friend, "cast", "heal", hero)
        elif friend.type == "archer":
            hero.command(friend, "attack", hero.findNearestEnemy())
hero.moveXY(126, 29)
while hero.time < 94:
    hero.shield()
    for friend in hero.findFriends():
        if friend.type == "paladin":
            if (friend.canCast("heal")):
                hero.command(friend, "cast", "heal", hero)
        elif friend.type == "archer":
            hero.command(friend, "attack", hero.findNearestEnemy())
hero.moveXY(170, 34)
for i in range(3):
    hero.summon("griffin-rider")
hero.moveXY(173, 13)
hero.moveXY(204, 14)

hero.moveXY(245, 17)
hero.moveXY(246, 33)
hero.summon("griffin-rider")
hero.moveXY(275, 33)
hero.moveXY(277, 56)
for i in range(5):
    if pet.findNearestByType("warlock"):
        hero.attack(pet.findNearestByType("warlock"))
hero.moveXY(277, 16)
for i in range(5):
    if pet.findNearestByType("warlock"):
        hero.attack(pet.findNearestByType("warlock"))
while hero.time < 107:
    hero.attack(hero.findNearestEnemy())
    for friend in hero.findFriends():
        if friend.type == "paladin":
            if (friend.canCast("heal")):
                hero.command(friend, "cast", "heal", hero)
        else:
            hero.command(friend, "defend", hero)
hero.moveXY(262, 12)
while hero.time < 185:
    if friend.findNearestEnemy():
        hero.command(friend, "attack", friend.findNearestEnemy())
hero.moveXY(277, 32)
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    for friend in friends:
        hero.command(friend, "attack", enemy)

Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time! :partying_face:

It’s late at night for many people, so you’ll have to wait awhile before some of the good coders come on :confused:. Sorry.

Welcome to the forum Orville_Cheun! :partying_face: Hope you enjoy your time here!

I believe you did this many times, so instead of writing this piece of code over and over again, try making a function and putting these lines in it, because I assume this is slowing your code down.

Plus in Python, I think that there shouldn’t be any parenthesis for the if condition statement like here for example:

it would be better this way: if friend.canCast("heal")

In addition, over here, friends is not defined:

And an advice, try storing variable instead of writing things over and over again, like
enemy = hero.findNearestEnemy()
warlock = hero.findByType("warlock")
friends = hero.findFriends()

I would recommend using while True loops. And use more functions. It makes your code cleaner so it will be easier to debug.
By The Way, I think your code crashed because you used too much while loops.

Didn’t you just say he needs to have while true loops?

Sorry, bad wording. put the code-inside the while loop into functions, and put the functions in the While-True loops.

1 Like

“put the code-inside the while loop into functions, and put the functions in the While-True loops” - this will only complicate the problem
CoCo often throws infinite loop error when using this construct:

while hero.time < time:
    # code
# replace it with
while True: 
    if hero.time < time:
       # code
    else:
        break

# modified some bits of your code
def gate1():
    hero.moveXY(92, 33)  # position mod
    # your code
    # in the first battle you have lost too many units

# when fighting towers
hero.moveXY(130, 32) # mod
while True: 
    if hero.time < 60:
        hero.shield()
        for friend in hero.findFriends():
            if friend.type == "paladin":
                if (friend.canCast("heal")):
                    hero.command(friend, "cast", "heal", hero)
            elif friend.type == "archer":
                hero.command(friend, "defend", {"x":111,"y" :32}) # command and pos mod
    else:
        break

# but this loop and all other loops can be much better if written as
while True: 
    towers = hero.findByType("beam-tower")
    if len (towers) > 0:
        hero.shield()
        for friend in hero.findFriends():
            if friend.type == "paladin":
                if (friend.canCast("heal")):
                    hero.command(friend, "cast", "heal", hero)
            elif friend.type == "archer":
                hero.command(friend, "defend", {"x":111,"y" :32})
    else:
        break
# in both cases no infinite loop, result:


the following battles are not so hard…