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 trialRichard Hummel
Courses Plus Student 5,677 Pointswhy use len(list(secret_word)) in 2nd while loop? c
Can someone explain the thought process of why we do this in the letter game section of the course?
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsIn code, the len(string)
is always the same as the len(list(string))
.
In looking at the teacher's notes for the previous video letter game introduction, Kenneth says:
Did you notice?
There's a tricky condition (on purpose) in the final version of our code from this video. Words that have repeated characters won't be marked as correct once they're all correctly guessed due to our len() comparisons. See if you can find a way to fix that yourself!
This issue is if there are repeated letters in secret_word
, the number of guesses will never match the total letters of secret_word
, The solution would be to make a set
out of it, so you are comparing:
len(good_guesses) == len(set(secret_word))
>>> a = "blueberry"
>>> len(a)
9
>>> len(set(a))
6
So not explicitly stated, I believe Kenneth meant to use set
instead of list
A B
1,146 PointsThanks for the explanation here. It would be awesome to be able to bookmark explanations like this for future reference.
MINJUN MOON
7,723 PointsMINJUN MOON
7,723 PointsThanks! Chris Freeman!