2
Now that we have over a hundred ways to get stuck in a loop, the next challenge is to get out.
Specifically, write a piece of code with a (seemingly) infinite loop, but somehow manage to break out.
The "infinite loop" can be a statement like while true
, for (;1>0;)
or anything else which obviously lasts forever. Recursion does not count (since stack overflows will quickly occur).
To break out, you need to execute an instruction written just after the loop. You can not just exit the program or leave the method.
Valid answer (if only it somehow exited the loop):
while (true)
print("I'm stuck!")
print("I escaped!")
Invalid answer:
define loop() {
while (true)
print("I'm stuck!")
}
loop()
print("I escaped!")
2I think this should be tagged with underhanded – William Barbosa – 2014-07-21T16:46:23.570
Are we allowed to use something such as
break
in python? You didn't seem to exactly specify that. – DatEpicCoderGuyWhoPrograms – 2014-07-21T18:02:37.340@DatEpicCoderGuyWhoPrograms it is a popularity contents. My guess
break
wouldn't be very popular. – Danny – 2014-07-21T19:46:46.1771I must be missing something, I don't understand how your "Valid answer" answers the question. How does the
print
statement break out of the loop? – Greg Hewgill – 2014-07-21T20:03:44.0371why is this marked as duplicate? the questions are intirely different. for example, no answer for this question is a valid answer for the other question, nor any answer for the other question is valid here. – proud haskeller – 2014-07-21T21:39:49.640
@Greg clarified. – Ypnypn – 2014-07-21T22:11:17.287
@proudhaskeller My comment explaining why I decided to close-vote as (indirect) duplicate was automatically removed when the 5th vote came in. My argument is that most of the answers to the other question probably fail at some boolean check when trying to get out of a loop. All you need to do is invert that boolean and you have a valid submission. – Martin Ender – 2014-07-21T22:12:02.433
@MartinBüttner You can assert that but, there are other very creative ways of doing what this question asks as shown by the answers below. This is not a duplicate. – gxtaillon – 2014-07-22T00:42:25.530
@gxtaillon well out of the current answers, the only valid and interesting ones could be trivially adapted to be valid answers to the other question. The guidelines for "is it a duplicate" are not "Is it a different question?", but "Can answers be copied over without significant change?" which is definitely the case for many answers. – Martin Ender – 2014-07-22T07:08:39.080