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 trialThelma Boamah
15,726 PointsWhy don't you have to set word as a variable before you can use it in the for loop to print(word) in my_list?
In JavaScript for example, you'd have to set word to an index value and loop through that index value. Does Python just know to do this automatically? Apologies if this is explained later in the video.
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsThe for...in
statement takes care of the assignment for you. It says "for each item in the container (my_list), assign the item to the variable 'word' then run the code block. Once complete, repeat the loop with the next item in container assigned to 'word'
"
Thelma Boamah
15,726 PointsThelma Boamah
15,726 PointsThanks for clearing that up, Chris.
Sky Lu
1,260 PointsSky Lu
1,260 PointsHi Chris,
If I try below codes:
but , as my understood above your reply, if I type "list", it should return
but it actually just return
5
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsSky, it uncertain the exact intention of your code due to the indention issues. Another issue is using the built-in name
list
as a variable name. Using "list" as a variable name overwrites the object in the local name space removing thelist
functionality.If I reformat you code, I get
Notice that
item
retains the value from the last iteration of the loopI cannot figure out how you got the result "5". A more explicit and accurate screen capture would be needed.