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 Collections (Retired) Lists Redux Disemvoweled

Why does the variable work the way it does?

I see that the variable 'state_names' is defined, but on line 5 the variable 'state' is used alone. Why does it recognize 'state' as the states when you never defined the variable 'state = ("Alabama", "etc")'. I am new to coding and it seems odd that it recognized that variable when it was specifically defined (although 'state_names' was).

This is also true with 'vowel'. You defined 'vowels = list('aeiou')' but never defined 'vowel' (without the 's').

In earlier sections if I used a variable without an 's' when my variable had an 's' it would cause an error.

Am I missing something here?

I hope my questions make sense.

Thanks.

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Jamison.

Those 2 cases are 2 loops. We use loops to go check and go through all the elements of the instance that we are looping.

For example when you speak about the state variable without "s" and saying it is never defined:

You are right it was not defined. Actually it is inside a loop that wants to go through all the elements of state_names. The teacher here called it "state" because if makes sense, it actually is a singular state (referring to every element in state_names), but you could call it the way you want.

The compiler will automatically give a value to state according to what is stored inside of state_names.

In a loop the value will firstly be the first item in the list, then the second item of the list that we are looping and so on...

The same FOR vowel IN vowels, which is another FOR IN statement.

Let me know if clearer

Vittorio