PHP Challenge Solution

Alan T.
Alan T.
3,304 Points

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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,289 Points

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.