Stop infinite loops

Is there a way to make stop infinite loops? They are really annoying.

Yes, you can either reset the level, or type return at the very top of the code or to solve the problem that is causing the infinite loop
Lydia

@lukas31 I think you can put pass at the end of the code to start up new loop or just pass to end the loop if you mean like stop then yes reload your website. But what do you mean there is usually a time limit for levels could you please give us more information and insert your code you used in here. Thanks and I will help the best I can. :smile:

It doesn’t work. Here is my code

def attack(enemy):
    if enemy and enemy.type != "yak":
        if hero.canCast("summon-burl"):
            hero.cast("summon-burl")
        if hero.canCast("poison-cloud", enemy):
            hero.cast("poison-cloud", enemy)
        if hero.canCast("drain-life", enemy) and hero.distanceTo(enemy) < 20:
            hero.cast("drain-life", enemy)
        if hero.canCast("earthskin", hero):
            hero.cast("earthskin", hero)
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
        hero.attack(enemy)
    


def skeleys():
    if hero.canCast("summon-undead"):
        hero.cast("summon-undead")
    for skele in hero.findFriends():
        enemy = hero.findNearestEnemy()
        if enemy and (skele.type == "skeleton"):
            hero.command(skele, "attack", enemy)
        if skele.type == "skeleton" and not enemy:
            hero.command(skele, "defend", hero)
    


def raise_dead():
    Summon = hero.findCorpses()
    toSummon = hero.findNearest(Summon)
    if toSummon:
        if hero.canCast("raise-dead"):
            hero.cast("raise-dead")


def summon():
    if hero.gold > hero.costOf("archer"):
        hero.summon("archer")
    for solider in hero.findFriends():
        enemy = hero.findNearestEnemy()
        if enemy and (solider.type == 'archer'):
            hero.command(solider, "attack", enemy)
        elif solider.type=="archer" and not enemy:
            hero.command(solider, "defend", self)
    

while True:
    enemy = hero.findNearestEnemy()
    skeleys()
    attack(enemy)
    summon(enemy)
    raise_dead()
    skeleys()
    pass

Sorry. Accidentally clicked solution. :frowning:

what level is it I need to know what your supposed to be doing just tell me the level and I can figure it out.

Sarven Brawl 5 (20 chars)

try changing this because this makes it think of it as an infinite loop for some reason I just tested it.

that self.findNearest(self.findEnemies())

Where do I put it? (20 chars)

what language is this?

Python. (20 chars)
Thanks

okay instead of putting so many if statements put and if followed by a else for some of your spells if this does not work ill help tmr

Okay, (20 chars)
Thanks

It works now! Thanks for your help!

def run():
    while True:
        enemy = hero.findNearestEnemy()
        skeleys()
        attack(enemy)
        summon(enemy)
        raise_dead()
        skeleys()

I just did this, but thanks again for your help!

2 Likes

So getting rid of some of the if’s did work? Good and your welcome. Good luck!

This either has to be in an if statement or an elif or an else.
Lydia

Yes true. I did not see that I just saw that he needed to get rid of some of his if’s because he had many.

you could also technically make the loop detect infinite loops and end by making a variable that saves the amount of times the loop has been run and end the loop when it exceeds a maximum amount of runs.