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

Infinite loop in Blind Distance (Python) SOLVED

I was teaching my kid to code with codecombat.com, and we have reached Blind Distance level. I was trying to explain to her that what would happen if you don’t add the if statement in the code but just the hero.say(dist).

When we ran the level, what happened is that the game froze (nothing happened) and after a while, there was a duck saying the code is either too slow or contains an infinite loop.

All we did is add hero.say(dist), nothing else. I was expecting to see “0” said by the character.

Is this expected behaviour? It makes no sense to me.

EDIT:

I think I figured it out. hero.say(0) is actually a special case and does nothing. This leads to a problem that it creates a program that runs too long (as the program seems to yield to other threads/processes only when it contains some meaningful action like say or moveXY).

This is very unfortunate because it makes tinkering around the program quite hazardous. If you omit the if statement, you get error about broken program code while it really is not - at least not on the level that student understands.

You can fix this issue by adding str() inside the brackets like this:

hero.say(str(distance))

or

hero.say(str(0))
1 Like