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

How does this figure out if there are duplicate letters in the same word? A few other questions as well.

Full code can also be viewed here under the file' letter_game.py':

https://w.trhou.se/k98lvoruy8

Question 1:

So I read through all other questions and what I don't understand is, how does what Kenneth wrote figure out if the guessed letters appear twice?

For example, the word "cool" has two 'o's in it. In the last video, the way the code was written, you wouldn't be able to finish the game if you had a word with two duplicate letters, like "cool".

In this video, the code resolves the issue. However, I don't understand how. I'm assuming it's this bit of code Kenneth has written here:

while True:
    draw(bad_guesses, good_guesses, secret_word)
    guess = get_guess(bad_guesses, good_guesses)

    if guess in secret_word:
        good_guesses.append(guess)
        found = True
        for letter in secret_word:
            if letter not in good_guesses:
                found = False
        if found:
            print("You win")
            print("The secret word was {}.".format(secret_word))
            done = True 

The found variable seems to be doing something but I can't figure it out which leads me to my second question below.

Question 2:

Following onto the first question, how and what is the purpose of the found variable? Is this the key for determining duplicate letters? If not, what's its purpose?

1 Answer

Steven Parker
Steven Parker
231,261 Points

1) The win determination is made with this loop:

        for letter in secret_word:
            if letter not in good_guesses:
                found = False

This loop checks every letter of the chosen word to see if it has been guessed (and put into good_guesses). If any letter has not been guessed, then the value of found is set to False. Using this win-checking strategy, it does not matter if the word has duplicate letters or not.

2) After the loop finishes, the found variable will have the result of checking if all letters of the word have been guessed. If they have, it will be True and the player wins. Otherwise, it will have False.