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 trialShea Taylor
Courses Plus Student 664 PointsKenneth Love's Devowel script
I'm a little confused regarding a few keywords in this script. Full script:
state_names = ["Alabama", "California", "Oklahoma", "Florida"]
vowels = list('aeiou')
output = []
for state in state_names:
state_list = list(state.lower())
for vowel in vowels:
while True:
try:
state_list.remove(vowel)
except:
break
output.append(''.join(state_list).capitalize())
print(output)
This specific part (code modified to assist the question). Why does this print out the states in a list when we haven't defined what state
is?:
state_names = ["Alabama", "California", "Oklahoma", "Florida"]
vowels = list('aeiou')
output = []
for state in state_names:
print(state)
Also, same instance here. Does Python know what vowel
means? I'm not sure why this is working since we haven't defined the word vowel.:
for vowel in vowels:
while True:
try:
state_list.remove(vowel)
except:
break
Just trying to wrap my head around the details. Any clarification would be much appreciated.
(Kenneth Love )
1 Answer
Ken Alger
Treehouse TeacherShea;
Welcome to Treehouse!
You ask some great questions about for
loops. The variable names Kenneth decided to use, state
and vowel
could have been anything. Pythonβs for
statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. The iteration variable, state
and vowel
in these instances, are essentially a holder variable. As stated, they could have been named x
and y
, or python
and java
, or cat
and dog
. However, using variable names that have meaning to the situation helps everyone understand what is going on.
In the state_names
example, the for
loop goes through each item in the state_names
list and does something with it. So the first iteration through the for
loop, state
will have a value of "Alabama", the second time through "California", etc. The same thing holds true for the for
loop with vowel
.
See how the naming convention Kenneth used helps? If he had used x
and y
, which is popular with some people, it wouldn't be nearly as readable or understandable.
Post back if you are still stuck.
Happy coding,
Ken
Shea Taylor
Courses Plus Student 664 PointsShea Taylor
Courses Plus Student 664 PointsKen Alger That makes perfect since! The
for
loop is much less mysterious now. Thank you!