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 trialAngelique Pat
1,996 PointsWhy do we need to do [keeper -1] to get the goal keeper's name?
Partial code:
keeper = input("Please select the goal keeper by selecting the player number . (1-{})".format(len(players)))
keeper = int(keeper)
print("Great!! The goalkeeper for the game will be {}.".format(players[keeper-1]))
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsSince Python (and most other modern languages) have 0 based arrays/lists, the first player's index would be zero. So if you wanted the first player it is stored in players[0]
. By the way, you're going to see this quite a bit if you program in Python and Javascript!
Most people if presented with a user interface and want the first player, they will type in the number 1. We have to compensate for the zero based array and do the subtraction for them!
Hope this makes sense for you.
Jonathan Jimenez
Python Development Techdegree Student 1,970 PointsJonathan Jimenez
Python Development Techdegree Student 1,970 PointsOne of the reason is to keep your code simple and short following the idea of DRY ( Don't Repeat Yourself) As well it will select the last index (player) inside the keeper list