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 trialIain Simmons
Treehouse Moderator 32,305 PointsMy 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!
Let me know what you think!
1 Answer
Kenneth Love
Treehouse Guest TeacherNice work!
Try not to use global
, though. Scope is important and thinking about it will make you a better programmer.
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsYeah, 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?Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherDefine it in the global scope?
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsDo 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?
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsOkay so I moved the code from
main()
into awhile
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