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 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.