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,581 Points

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

Steven Parker
Steven Parker
201,382 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
201,382 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
201,382 Points

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

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