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

Letter_Game

while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
    for letter in secret_word:
        if letter in good_guesses:
            print(letter, end='')
        else:
            print('_', end='')

1)In the letter game I was unable to understand why are we comparing length of good_guesses to len(list(secret_word)) 2)what is list(secret_word) do ? I understand that this is checking for the length of secret_word but what does the list(secret_word) means and 3)let's say user input is apple which means length of good_guesses is 5 and assume that secret_word is also apple in this case len(good_guesses) is equal to len(secret_word) but why are we saying len(good_guesses) != len(list(secret_word)) 4) why are we saying end = '' you mentioned in the video saying this is to create spaces but when we look in to the list of words we don't see any spaces between letters for example the words = ['apple','kiwi'] I don't see any spaces between letters in word 'apple' 5)in else block you're using print('_') what is this for ? can you help me understand ? Thanks, Srikanth Soma.

Steven Parker
Steven Parker
243,201 Points

Try formatting your code so the spacing shows up (in Python, indentation is everything).
Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

Thanks I just updated my question.

1 Answer

Steven Parker
Steven Parker
243,201 Points

OK, here are the answers to your questions:

1) In the letter game I was unable to understand why are we comparing length of good_guesses to len(list(secret_word))

If you have guessed all the letters, you win. So this is a way to test for a win, but be aware that it only works for words that have all unique letters. If the test has duplicate letters, this test will fail even when you have won.

2) what is list(secret_word) do ?

That converts the string into a list, but it's not really needed here since you can get the len() of a string directly.

3) let's say user input is apple which means length of good_guesses is 5 and assume that secret_word is also apple in this case len(good_guesses) is equal to len(secret_word) but why are we saying len(good_guesses) != len(list(secret_word))

The user input is just one letter at a time. And "apple" is one of the words that the current logic will not work on, since you can win in 4 guesses but the word has 5 letters. But what this test is intended to do is end the game if you have a win.

4) why are we saying end = '' you mentioned in the video saying this is to create spaces but when we look in to the list of words we don't see any spaces between letters

The "end=''" option is to not create spaces. It makes sure that the next print will come directly after this one. And this is not related to the list of words, this is only about what the program shows to the user.

5) in else block you're using print('_') what is this for ?

If the letter has already been guessed, it is shown by the first print. But if it has not been guessed, this statement shows an underscore ("_") instead of the letter.

Steven Parker 1) Question to your answer for example list = ['apple'] user input is 'a' so len(good_guess) is 1 continue untill user input 'e' now the len(good_guess) is 5 agree ? and assume secret_word is 'apple' len(secret_word) is also 5 so why are we comparing len(good_guess) != len(secret_word) ? can you help me understand ? 5) question to your 5th answer based on logic above I pasted my understanding is if the user input letter is in secret_word then print that letter to console output if not in secret_word then else block with execute and prints '_' but you're saying opposite correct me if I'm wrong

Steven Parker
Steven Parker
243,201 Points

You asked:
Question to your answer for example list = ['apple'] user input is 'a' so len(good_guess) is 1 continue untill user input 'e' now the len(good_guess) is 5 agree ?

No, if the second guess is 'e' then len(good_guess) would then be 2.

and assume secret_word is 'apple' len(secret_word) is also 5 so why are we comparing len(good_guess) != len(secret_word) ?

As I said before, 'apple' is not a good word for this program, you would need different testing logic to work with a word that has repeated letters. If the word had unique letters, such as "orange", then this code would test that the user has not won yet.

question to your 5th answer based on logic above I pasted my understanding is if the user input letter is in secret_word then print that letter to console output if not in secret_word then else block with execute and prints '_' but you're saying opposite correct me if I'm wrong

All of the letters will be in "secret_word", what the code is testing is if the letter is in good_guesses or not.

So if the user input letter is in secret_word good_guesses then print that letter to console output if not in secret_word good_guesses then else block will execute and print '_'