为什么我的代码会打印列表中的所有名称,而不仅仅是玩家1:名称?

时间:2019-03-22 10:21:36

标签: python

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

# 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_players = input("Would you like to add a player to the list? (Yes/No) ")
while add_players.lower() == "yes":
  name = input("\nEnter the name of the player to add to the team: ")
  players.append(name)
  add_players = input("Would you like to add another player? (Yes/No)")


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

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

# TODO Select a goalkeeper from the above roster
keeper = input("Please select the goal keeper by selecting the 
player_number. (1-{})".format(len(players)))

# 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(players[keeper-1]))

我希望列表显示像 玩家1:查理 玩家2:约翰 玩家3:汤米 但是当前代码向我显示了此输出: 1

1 个答案:

答案 0 :(得分:0)

您应将players替换为player

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