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 Introduction

2 Answers

andren
andren
28,558 Points

The code that is responsible for printing out the secret word is this:

for letter in secret_word:
    if letter in good_guesses:
        print(letter, end='')
    else:
        print('_', end='')

It starts by looping through the secret_word string pulling each letter into a variable called letter, then within that loop it checks if the current letter exists within the good_guesses list. If it is present in the list the letter is printed out, if it is not present in the list then an underscore is printed out instead.

When the game first starts the good_guesses list is empty so all letters will be printed as underscores, once you make a guess that is correct that letter is added to good_guesses. Which causes that letter to be printed out while the other letters which have not yet been guessed will still be printed as underscores

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good question. In the video at 6:00 mark, Kenneth adds a for loop to check if each letter of secret word is in the guessed letter list. If the letter has been guessed it gets printed, otherwise an underscore is printed.

Post back if you need more help. Good luck!!!