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 trialJing Zhou
2,162 PointsFor letter in secret_words, Is the letter a variable or does it have special meaning?
For letter in secret_words,
Is the "letter" in this line means something special or just a variable name? In other words, if I change it to a different name, Is it still gonna work?
2 Answers
Jennifer Nordell
Treehouse TeacherThat's exactly right! The letter
is just a variable name. It will be used when accessing each individual item in that list. Kenneth just picked a variable name that made sense. Here are some other examples of things that make sense.
for fruit in fruit_basket:
for book in reading_list:
Each iteration through the loop will look at the next individual item and assign the value to the variable name after the for
.
Hope this helps!
Jing Zhou
2,162 PointsI think I understand now! Just to confirm, so the "For xx in xx" structure is not just used to pick things from a list, it can be a string as well? And for string, it automatically consider each letter in the string, correct?
Jennifer Nordell
Treehouse TeacherYes. Another way to say it would be each individual item in an iterable. And a string is iterable in Python. Which means that in the case that the iterable is a string the "individual" item will be a character of the string
Jing Zhou
2,162 PointsThank you so much, Jennifer. You are a great help!
Jing Zhou
2,162 PointsJing Zhou
2,162 PointsThank you! I'm still a little confused. The "secret_words" is a list of words, not "letters", but when you say: letter in secret_words, you actually inspecting each letters in a word? Does my question make sense?
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherI would say that you misread the code. The actual code is:
for letter in secret_word:
The value stored in
secret_word
is a string that was randomly selected from this line:secret_word = random.choice(words)
This line pulls a random word from this list:
So the value stored in
secret_word
is one string such as "apple". So for every letter in that string or:for letter in secret_word:
Make sense? Hope this helps!