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 Takeaways

My letter game (with file imports!)

So here's my letter game (created mostly before watching the videos), where you create a file of words separated by new lines and it uses that as the list for the random choice.

Also, if the randomly selected word is less than 7 characters long, you get 7 guesses, otherwise you get 10. Not sure if that's entirely fair, but that's how it goes!

iainsimmons/letter_game

Let me know what you think!

Kenneth Love

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Nice work!

Try not to use global, though. Scope is important and thinking about it will make you a better programmer.

Yeah, I was trying to avoid it, but without using classes or passing the whole words list to every function, I couldn't think of how to do it.

Any suggestions? Is there a way to have a function that is just used to store and return a variable that it holds the value for? I don't know much about yield but maybe it could be used?

Do you mean define the variable in the global scope with the list of words from the file, rather than doing that in one of the functions? So... would that just be ripping everything out of the main() function, and putting that in the global scope?

Or did you mean defining a function on the global scope to store and return a variable?

Okay so I moved the code from main() into a while loop so I could repeatedly prompt for another word list file if an exception was raised. Thanks for the tip!

Removed 'global' and put main() code in loop | iainsimmons/letter_game