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

PHP Challenge Solution

Undefined "player" in "for player in players:"?

Hi, I'm new to Python but I'm not sure why in Ken's tutorial, he uses code that says:

for player in players:

However, he hasn't defined what "player" is, and so I'm not sure how Python knows what he's referring to. Can anyone help me with this?

1 Answer

The Python doc explanation

A less complex tutorial

Or a stackoverflow question where you find (partial answer):

The left expression of a for loop statement gets assigned with each item in the iterable on the right in each iteration, so

for n in a:
    print(n)

is just a fancy way of doing:

for i in range(len(a)):
    n = a[i]
    print(n)

The variable can have any valid variable name so for example

for x in players:
    print(β€œPlayer {}: {}”.format(player_number, x))

would function the same as in the video.

From the stackoverflow answer if a is a list a[i] is an item in the list. If a is a string a[i] is a character in the string.