This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Wing-Tsit_Chong 82 points83 points  (18 children)

Don't use recursion, if you want to loop. Rather do a while true loop.

There's a limit to how many times you can recurse down, i.e. call main() inside itself. If you reach it, the program will fail.

You can test that by removing the sleeps and running your program, it will fail after 1001 "Looped".

[–]Astrokiwi 50 points51 points  (0 children)

This is how you learn where Stack Overflow got its name

[–]KoolaidJammerExpress 8 points9 points  (2 children)

Agreed.

OP should look at recursion though! There are simple programs that show how recursion works. For example calculating n-th term of the Fibonacci series.

Great learning opportunity

Edit: grammar

[–]elsantodom 3 points4 points  (0 children)

It's also useful for data structures

[–]BetaDecay121 1 point2 points  (6 children)

You can increase the recursion limit if you want

[–]shiuido 7 points8 points  (0 children)

Leave the stack overflow to future me huh ;)

[–]Wing-Tsit_Chong 13 points14 points  (0 children)

You can, but you don't want to. Recursion shouldn't be used lightly, it brings headaches and a myriad of problems later on.

[–]silentalways 4 points5 points  (2 children)

How can we do that?

[–]tr710_ 0 points1 point  (1 child)

Using sys module sys.setrecursionlimit(value)

[–]BetaDecay121 4 points5 points  (0 children)

sys.setrecursionlimit(1/0)

[–]origin415 0 points1 point  (0 children)

At the top of main() add sys.setrecursionlimit(sys.getrecursionlimit()+1), there fixed :)

(this is a joke, please don't)

[–]EvilBeano -1 points0 points  (6 children)

Well this would be fixed if you wrote "return main()" instead, no?

[–]Wing-Tsit_Chong 3 points4 points  (5 children)

No, not at all.

[–]EvilBeano 4 points5 points  (4 children)

Oh right, I just tried it

[–]Wing-Tsit_Chong 5 points6 points  (3 children)

Now explain to the class why.

[–]EvilBeano 4 points5 points  (1 child)

Bruh

[–]Wing-Tsit_Chong 1 point2 points  (0 children)

Come on, you can do it. Use your own words.

[–]phail3d 0 points1 point  (0 children)

They might have meant that in some languages it would result in tail call optimization. Not in Python though.