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 trialMark Tripney
6,009 PointsSolution using enumerate...
A slightly different solution, using enumerate
to number the roster.
player_names = []
active = True
while active:
prompt = input("Would you like to add players to the list? ")
if prompt.lower() == "y":
prompt_name = input("OK. Please enter a name: ")
player_names.append(prompt_name)
else:
print("Roster:")
for number, player in enumerate(player_names, 1):
print("\t" + str(number), player.title())
print("There are", len(player_names), "players on the team.")
active = False
keeper = int(input("Who is the goalkeeper? "))
print(player_names[keeper - 1].title(), "is the keeper.")
Ty Yamaguchi
25,397 PointsCool idea using enumerate. much cleaner than keeping a separate counter going!
1 Answer
Mark Tripney
6,009 PointsHi Gideon,
The \t
adds a 'tab' to the string when it's printed to the screen, so it's just a little formatting thing. Your code looks like it should work fine - keeper
will be a string, though, not an integer, so you might want to wrap it in int
...
keeper = int(input("Please select a goalkeeper using the player number. (1-{})".format(len(player_names))))
Gideon De Villiers
Courses Plus Student 632 PointsGideon De Villiers
Courses Plus Student 632 PointsHi Mark,
Thanks for showing me how to use an enumeration. Can you explain the purpose of "\t"? Also, the only feedback I have is that the input for keeper invites the user to type in the name of the player rather than the number. Would you still use the following code to invite the user to enter an int?
keeper = input("Please select a goalkeeper using the player number. (1-{})".format(len(player_names)))