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

Jing Zhou
Jing Zhou
2,162 Points

For 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

That'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! :sparkles:

Jing Zhou
Jing Zhou
2,162 Points

Thank 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I 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:

words = [
    'apple',
    'banana',
    'orange',
    'coconut',
    'strawberry',
    'lime',
    'grapefruit',
    'lemon',
    'kumquat',
    'blueberry',
    'melon'
]

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! :sparkles:

Jing Zhou
Jing Zhou
2,162 Points

I 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yes. 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 :smiley:

Jing Zhou
Jing Zhou
2,162 Points

Thank you so much, Jennifer. You are a great help!