I'm trying to solve the level Shine Getter, but an infinitte loop error keeps popping up and Tharin won't move!
Can any of you guys help me out on this?
while True:
coins = hero.findItems()
coinIndex = 0
while coinIndex < len(coins):
coin = coins[coinIndex]
if coin.value == 3:
hero.moveXY(coin.pos.x, coin.pos.y)
pass
Try putting an if statement in front of the if coin.value == 3 because you’re not checking if there’s a coin before you’re checking it’s value.
Hope this helps
This is what appeared:
To exit from a loop you need to increment its index.
Thanks, I needed that!
The same thing is happening to me on this level where there is an infinite loop and I can’t see why. Here is my code:
while True:
coins = hero.findItems()
coinIndex = 0
# Wrap this into a loop that iterates over all coins.
while coinIndex < len(coins):
coin = coins[coinIndex]
# Gold coins are worth 3.
if coin.value == 3:
# Only pick up gold coins.
hero.moveXY(coin.pos.x, coin.pos.y)
coinIndex += 1
pass
Define coin before you use it in anything. you need a while true loop that the while coinIndex < len(coins):
is inside. inside the while true loop, before the while coinIndex < len(coins):
loop, define coin.
uumm for me he just stays there`
while True:
coins = hero.findItems()
coinIndex = 0
# Wrap this into a loop that iterates over all coins.
while coinIndex < len(coins):
coin = coins[coinIndex]
# Gold coins are worth 3.
if coin.value == 3:
hero.moveXY(coin.pos.x, coin.pos.y)
# Only pick up gold coins.
coinIndex += 1
pass
Help please
The code is fine and works for me. What equipment are you using?
Using your code and your hero with same equipment, it passes for me. You’re saying that when you click run, nothing happens? He just stands in one place and does nothing?
Sorry I took so long but yes
First, try a different web browser. Which browser are you using now?
But thank you for your concern
I know this is an old topic, but seeing as there wasnt really a solution, I am adding this:
I see what my problem is: the hero goes straight for the coin nearest to him (in my case the one always on the direct right of the hero), for some reason, and identifies only that coin as his current target, and if that coin does not happen to be a gold coin, he just sits there infinitely.
So I used some old code for levels with neutral Yaks and turned it into finding coins.
while True:
non_yaks = []
for enemy in hero.findEnemies():
if enemy.type != "sand-yak":
non_yaks.append(enemy)
enemy = hero.findNearest(non_yaks)
if enemy:
hero.attack(enemy)
pass
while True:
coins = []
for item in hero.findItems():
if item.value == 3:
coins.append(item)
coin = hero.findNearest(coins)
if coin:
hero.moveXY(coin.pos.x, coin.pos.y)
pass
pass
This one works
And yes, I see that there was some solution but that didnt work for me.