Python Challenge Solution

Name of goalkeeper not printing

# 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'
user = input("Want to add players (Yes/No)  ")
while user.lower() == "yes":
  name = input("Enter name of player  ")
  player_names.append(name)
  user = input("Want to add more players?(yes/no)  ")


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

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

# TODO Select a goalkeeper from the above roster
goalkeeper = input("Select a goalkeeper from (1-{}) ".format(len(player_names)))
goalkeeper = int(goalkeeper)

# TODO Print the goal keeper's name
# Remember that lists use a zero based index
print("Goalkeeper name is {}".format(player_names[goalkeeper -1]))

2 Answers

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,333 Points

In your for loop player_name should be singular - or at least not the same name as your list

for player_names in player_names:
  print ("Player {} : {}".format(num,player_names))
  num += 1

should be

for player_name in player_names:
  print ("Player {} : {}".format(num,player_name))
  num += 1
player_names in player_names

# is incorrect. Try this instead:

for player in player_names:
  print ("Player {} : {}".format(num,player))
  num += 1

# And keep the code DRY and clean

goalkeeper = int(input("Select a goalkeeper from (1-{}) ".format(len(player_names))))
print("Goalkeeper name is {}".format(player_names[goalkeeper - 1]))