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

"player" in the for loop

I, like a few others, also struggled with figuring out how to increment the player numbers.

But, my specific question is regarding Ken's for loop... "For player in players"

Why it wasn't it necessary to define "player" in any of the previous code? It seems like we should've needed to define it before using it in the loop...

1 Answer

Steven Parker
Steven Parker
243,656 Points

You might be used to another language. In Python, the term you name first in a "for" loop is a variable that is created (or overwritten) as the loop starts, and is used to refernce the current iteration element. So it's not necessary to define a variable by that name in advance.

Got it! Thanks Steven!