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! While you're at it, check out some resources Treehouse students have shared here.

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

Having trouble with add_player

The code runs but when input for adding a new player is shown, when I type yes it gives a name error that add_player is not defined. How to resolve ?

Robin Goyal
Robin Goyal
4,582 Points

Can you post your code? It'll be easier for us to help you out.

Steven Parker
Steven Parker
229,577 Points

More than "easier", posting your code makes it possible for us to help out. :wink:

players = []

add_player = input("Do you want to add a new player? (yes/no) ")

while add_players.lower() == yes: name = input("\n Enter the name of player to add to the team: ") players.append(name) add_players = input("would you like to add anotehr player? (yes/no) ")

player_number = 1 for player in players: print(f"Player {player_number,player}") player_number +=1

keeper =input(f"please select the goal letter by selecting the player number {1-len(players)}")

keeper = int(keeper)

print(f"great!! the goalkeeper for the game will be {(players[keeper-1])}")

I can only paste like this! I cannot add screenshot of my image code.

2 Answers

Steven Parker
Steven Parker
229,577 Points

It looks like you created a variable named "add_player" (singular) but then referenced "add_players" (plural). Did the message possibly say "add_players" was not defined?

And when posting code, use Markdown formatting to preserve your indentation.

Oh i forgot that. Thanks bro :)

I just changed the name add_players but didn't run the code. It again shows me the name yes is not defined. Better I rewrite code from scratch and try again.

Thanks for help. Happy coding for you too mate :)

Steven Parker
Steven Parker
229,577 Points

Did you notice that you had "add_players" in two places? :wink:

boi
boi
14,241 Points
players = []

add_player = input("Do you want to add a new player? (yes/no) ")

while add_player.lower() == "yes": 
  name = input("\n Enter the name of player to add to the team: ") 
  players.append(name) 
  add_player = input("would you like to add anotehr player? (yes/no) ")

else:


  player_number = 1 

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

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

  keeper = int(keeper)

  print(f"great!! the goalkeeper for the game will be {(players[keeper-1])}")

Your code was a little messy but I made a few changes and fixed it, I hope that helps.!!