Python Challenge Solution

eestsaid
eestsaid
1,311 Points

Explanation of for loop required

In the following ...

player_number = 1
for player in players:
    print("Player {}: {}".format(player_number, player))
    player_number += 1

... I generally understand how the forloop works but am unsure what "player in players" at line 2 is doing.

Thanks for your help.

2 Answers

Hello! to loop through a collection of elments in python u genereally use a for loop how a for loop works? lets say u have a list of 5 players. to loop through each element u start the for loop with the word for and then a placeholder which will each element on each iteration, u can name the placeholder in whatever name u want, then u state on which collection of elemnts u want to loop through, and this is ofcourse the players list so:

players = ["momo", "lolo", "mika", "momota", "momonga"]
for player in players:
   print(player)
eestsaid
eestsaid
1,311 Points

ok so in for player in players: "player" is just a placeholder and then we are directing the for loop to run on the list of "players" by stating in players ... is that correct?

Many thanks.

eestsaid
eestsaid
1,311 Points

actually - in the next video I watched Craig covered it perfectly.