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

fahad lashari
fahad lashari
7,693 Points

I am completely baffled. Please help

I attempted this code challenge myself. My code did everything perfectly except display the progress correctly. My own code would display the correct guesses and '_' in incorrect order.

For example if the word was 'apples' and the user guessed 'e' first and then 'p' and so own. my code would do something crazy like 'aelspp'. However Mr. Kenneth has managed to get the letters to appeare in the perfect order. I am really trying hard to understand this small piece of code here:

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

But i can't seem to wrap my head around how the letters appeare in the correct order along with the '_' as the user continues to guess.

how does this code ensure the letters are placed in the correct order. Can someone please explain this. It would help a ton.

Kind regards,

Fahad

1 Answer

Beshoy Megalaa
Beshoy Megalaa
3,429 Points

Let's run a small simulation on the for loop!

# for example
secret_word = "apple"
good_guess = ['p', 'e']

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

first iteration of for loop: letter = 'a' if a in ['p', 'e'] # No, the letter isn't printed else _ # Yes, the underscore is printed

second iteration of for loop letter = 'p' if 'p' in ['p', 'e'] # Yes, the letter is printed else _ # No, the underscore isn't printed

third iteration of for loop letter = 'p' if 'p' in ['p', 'e'] # Yes, the letter is printed else _ # No, the underscore isn't printed

fourth iteration of for loop letter = 'l if 'l' in ['p', 'e'] # No, the letter isn't printed else _ # Yes, the underscore is printed

fifth iteration of for loop letter = 'e' if 'e' in ['p', 'e'] # # Yes, the letter is printed else _ # No, the underscore isn't printed

The loop has printed: _pp_e If you have not included (end = "") in both of the print statement, then you would've had

_
p
p
_
e

Summary If the user guesses right, you append the guess into the list. This for loop takes each letter in the secret_word, and check it it exits(prints letter) or not.(prints _ ).

fahad lashari
fahad lashari
7,693 Points

Man I can't thank you enough for explaining this to me so clearly. Thank you! Even though I had moved onto the further courses I still couldn't understand this but I get it now.

Much appreciated, thanks for taking the time out to providing a detail answer as such.

Kind regards,