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

Dylan Cowling
Dylan Cowling
Python Web Development Techdegree Student 2,590 Points

Slightly Confused

How does the game know where to place the correct letters along the line? I just can't figure out what I'm missing.

2 Answers

andren
andren
28,364 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

Dylan Cowling
Dylan Cowling
Python Web Development Techdegree Student 2,590 Points

Thank you, you have made it very clear to me now. I 100% understand how it works now, success! :D

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 56,801 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!!!