Welcome to the Treehouse Community
Looking to learn something new?
Jamaru Rodgers3,046 Points
Why did he make the variable, "secret_word", a list when doing the comparisons in later logic? len(list(secret_word))??
Didn't grasp why he made the secret word variable a list, when the secret word is just a single word. He then compares the length of the list to another list (Blankguesses) which is an actual list. Can someone please help? Lol maybe the answer is right in my face.
Steven Parker221,293 Points
I'm also baffled by this logic, since the len() function works on both strings and lists. It might have been needed if it only worked on lists, or if the way it worked on lists was different that the way it works on strings. But since neither of those is the case, the intermediate conversion is not needed here.
You may want to report this as a video "bug" to the Support folks.
I had this same question (sorry for reviving the thread). Steven is probably correct, but I had a silly idea and was hoping for feedback/criticism. Note, I'm a novice. :_)
Let's look at the code.
secret_word = random.choice(words) good_guesses =  bad_guesses =  while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
When we read the logic of the while statement, the good/bad guesses are easily noted as lists due to the code being in the same block. The secret word is not so obvious. We see it's assigned to random.choice. We have to leave the parent block (while True:) to finally get to the type (think about how long this could take if this code was in a function buried elsewhere).
My thinking is that the choice to explicitly use list(secret_word) was not functional but for readability. When we read back this:
while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
The use of list() informs us that the type is not a list, but can be converted to one. In a 100 word file it's not that illuminating, but I imagine it could be in other situations!
Sorry for the wall of text, and I could be talking gibberish. I'd love to know if there is any Truth to this idea. ;)