Python Challenge Solution

Mark Tripney
Mark Tripney
6,006 Points

Solution 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:  ")
        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.")
Gideon De Villiers
Gideon De Villiers
Pro Student 630 Points

Hi 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)))
Ty Yamaguchi
Ty Yamaguchi
23,931 Points

Cool idea using enumerate. much cleaner than keeping a separate counter going!

1 Answer

Mark Tripney
Mark Tripney
6,006 Points

Hi 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))))