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

Oren Schindler
seal-mask
.a{fill-rule:evenodd;}techdegree
Oren Schindler
Python Web Development Techdegree Student 1,098 Points

For Loop

I’m not clear why ‘player’ is used in the for loop? It was never defined. Why wouldn’t you use For ‘name’in players print(“Player {}: {}”.format(player_number,name))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Found this video in the Practice Creating and Indexing Lists

The player gets defined in the for loop.

The line for player in players: sets the variable player to each value in players, in turn.

Is common format to use:

for single_loop_variable in many_items:
# like
for car in cars:
# or
for ox in oxen:

Post back if you need more help. Good luck!!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Not sure I’m following.

The for loop variable name is typically not used outside of the loop and is distinctly named so it’s easy to follow within the loop.

If name is used outside the loop, it’s value would be overwritten by the loop:

Type "help", "copyright", "credits" or "license" for more information.
>>> name = 'bob'
>>> namelist = ['alpha', 'beta', 'gamma']
>>> for name in namelist:
...     print(name)
... 
alpha
beta
gamma
# “bob” is lost
>>> print(name)
gamma