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 Refinement

Richard Hummel
PLUS
Richard Hummel
Courses Plus Student 5,677 Points

why 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
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

In 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

MINJUN MOON
MINJUN MOON
7,723 Points

Thanks! Chris Freeman!

Thanks for the explanation here. It would be awesome to be able to bookmark explanations like this for future reference.