Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Challenge Solution


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: ")
    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,870 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:


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

1 Answer

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

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. ._.