Python Challenge Solution

Dejan Delev
Dejan Delev
461 Points

prints out the names of all the names in the list instead of printing them out separately

my problem is with the FOR statement, when I want it to show the players on the team, it shows as Player 1: [all the names on the list], Player 2: [all the names on the list] etc.

# TODO Create an empty list to maintain the player names
player_names = []


# TODO Ask the user if they'd like to add players to the list.
# If the user answers "Yes", let them type in a name and add it to the list.
# If the user answers "No", print out the team 'roster'
add_player = input("Would you like to add a player to your team? Yes/No:  ")
while add_player.lower() == "yes":
    name = input("\nEnter the name of the player to add to the team: ")
    player_names.append(name)
    add_player = input("Would you like to add a player to your team? Yes/No:  ")

# TODO print the number of players on the team
print("There are {} players on the team.".format(len(player_names)))

# TODO Print the player number and the player name
# The player number should start at the number one
player_number = 1
for player in player_names:
    print("Player {}: {}".format(player_number, player_names))
    player_number += 1

# TODO Select a goalkeeper from the above roster
keeper = input("Select a goalkeeper by selecting the player's number. (1-{}) ".format(len(player_names)))

keeper = int(keeper)

# TODO Print the goal keeper's name
# Remember that lists use a zero based index
print("Great! The goalkeeper for the game will be {}.".format(player_names[keeper - 1]))

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,777 Points

The issue is that you are passing the entire list (player_names) to the format function. You should be passing player - that's the purpose of creating it as the for loop variable.

for player in player_names: # you want to use this player

print("Player {}: {}".format(player_number, player_names)) # player_names is the whole list

# change to
print("Player {}: {}".format(player_number, player)) # Should be player

:tropical_drink: :palm_tree:

Dejan Delev
Dejan Delev
461 Points

that makes a lot of sense! Thanks!

Can you explain for me about player lable because i didn't see player label before and now we used it in for loop. I really confused. <p>for player in player_names:</p>

Dave StSomeWhere
Dave StSomeWhere
19,777 Points

Cao Tru, The player variable is dynamically created in the for loop's scope as a nice/builtin feature of the for loop. It is common practice to use a plural on the list like player_names and then use (create on the fly) a singular value for the iterator - like player or player_name (without out 's').

# create an iterable, like a list
list_of_integers = [1, 2, 3]

# Loop with a for 
for loop_created_integer in list_of_integers:
    # do something with each integer value
    print("Check out this integer --> {}, from my list".format(loop_created_integer))

# outputs
Check out this integer --> 1, from my list
Check out this integer --> 2, from my list
Check out this integer --> 3, from my list 

Thanks Dave StSomeWhere really make sense

Carlos Muñoz Sanchezllanes
Carlos Muñoz Sanchezllanes
4,065 Points

Dave!! Thank you so much, this helped a ton!! My raccoon hands thank you!