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

Difficult challenge :/ I can usually get most of it but struggled. Still have questions after watching Ken's approach

I think the part I'm most confused on is:

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

I somewhat remember "+=" to add to the number. I thought we did this in the Ticket Project but looking through my notes and code I'm not finding it.

Would someone be able to explain why Ken used this?

1 Answer

Steven Parker
Steven Parker
229,732 Points

You're exactly correct. The += operator is known as the addition assignment operator and causes the value on the right to be added to (and stored back into) the value on the left.

In essence, variable += value is a shorthand for variable = variable + value.

As used here, it is incrementing the player number to display with each player in the list.