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

Jamaru Rodgers
Jamaru Rodgers
3,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.

2 Answers

Steven Parker
Steven Parker
229,982 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. ;)

Thanks

Steven Parker
Steven Parker
229,982 Points

If you find "len(list(secret_word))" more readable than "len(secret_word)", then perhaps that's the value in what otherwise seems to be unnecessary complexity. For me, the less complex form is more readable.