Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Letter Game Refinement

Chris Smith
Chris Smith
2,991 Points

Recursive call to play function?

Why are you using this construct to exit the play() function: return play(done=False)

This looks like a recursive call to itself that will never actually return, because you use the sys.exit() function to end the game, so surely this will just keep eating more and more memory each time the user plays another game?

Good question, I'm not even close to thinking about performance yet. Just struggling to get things to run. But I guess in general the idea of a recursive function is a bad thing?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Yes, a linear way of repetitively calling calling play() would be better than a recursive call. I believe it was used here more out of convenience and to introduce the topic of recursion rather than focus on optimizing code performance.

The default recursion limit is 1000 depending on your system as reported by sys.getrecursionlimit(). So after 1000 games it would crash.

nice answer