Python Challenge Solution

Challenge

I am getting a name error on this please assist

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

# TODO print the number of players on the team
print("There are {} playes 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,player))
    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)))

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

Moderator edited: Markdown added so that code renders properly in the forums.

Dave StSomeWhere
Dave StSomeWhere
19,760 Points

Could you please format your code so it is readable - check the Markdown Cheatsheet below :arrow_heading_down:

Also, I don't think player_number has a player attribute

print("Player {}: {}".format(player_number,player)) 
Istvan Nonn
Istvan Nonn
2,092 Points

After testing your code the error is in the following line:

keeper = input("Please select the goal keeper by selecting the player number. {1-{}}".format(len(players)))

The problem is:

{1-{}}

which will give you a ValueError not a NameError You need parentheses ( 1 - {} )

1 Answer

it should be print("Player {}: {}".format(player_number, players))

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

I think that would just give you the list, and it would print it out in each line in the for function. So you would want the use "player" thats going through players list to print out each thing in the list. ._.