Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Infinite loop please help

infinite loop why Idon’t know

click on “Comment Out My Code” and then it will open the level. Post your code here if you need help identifying the infinite loop.

while hero.now() < 30:
item = hero.findNearestItem()
if item:
if (hero.isReady(“jump”) and hero.distanceTo > 15):
hero.jumpTo(item.pos)
else:
hero.move(item.pos)
if (hero.gold > 59):
break

if (hero.isReady(“jump”)):
hero.jumpTo({‘x’: 21, ‘y’: 38})
hero.moveXY(21, 38)
while hero.pos.x > 16:
hero.move({‘x’: 15, ‘y’: 38})
hero.buildXY(‘fence’, 20, 38)[quote=“MunkeyShynes, post:2, topic:18903, full:true”]
click on “Comment Out My Code” and then it will open the level. Post your code here if you need help identifying the infinite loop.
[/quote]

Hi Hayden_Fung, please could you post your code with proper formatting.
Visit this topic to see how you do it: [Essentials] How To Post/Format Your Code Correctly
Thanks,
:lion: :lion: :lion:

while hero.now() < 20:
    # Собирай монеты
    #hero.say("Нужно собирать монетки")
    items = self.findItems()    
    gold = hero.findNearest(items)
    maxcost = gold.value/hero.distanceTo(gold)
    while len(items)>i:
        if items[i].value/hero.distanceTo(items[i])>maxcost:
            gold=items[i]
            maxcost = gold.value/hero.distanceTo(gold)
        i+=1;    
    self.move(gold.pos)
 
 
while hero.pos.x > 16:
    # Беги за ограду
    #hero.say("Пора бежать")
    self.move({"x":16, "y":38})
 
    
# Дострой стену, чтобы спрятаться от огров.
self.buildXY("fence", 21, 37

What level is this? This way, we can jump into the level to test your code to see what may be causing problems and give you specific help.

If you are trying to find the best value, you want to start with a empty values before the while loop. Otherwise you may distort the value before starting the while loop

gold = None
maxcost = 0
#don't forget to set your index
i = 0

ogre gorge gouger is the level.

The original code is running perfectly with the simple ( but crucial :slight_smile: ) addition of

i = 0
 gold = hero.findNearest(items)  # little bit risky if there is no gold
 maxcost = gold.value/hero.distanceTo(gold) # no gold.value and no distance to gold
# but runs for the first 20 seconds

this is the result of your logic if you collect gold for more seconds:


if you follow the Brooksy advice you’ll be able to collect all coins
ogre-gorge-gouger