Python

Shane Cookson
Shane Cookson
778 Points

My list is not adding in the item from my input

Im trying to get the users input from "name" and add it to my list called "team"

I am not sure why it is not adding to the list?

team = [] length = len(team) player_add = input("Would you like to add a player to the list? (Yes/No)")

while player_add.lower() == "yes":

name = input("Enter the name of the player you would like to add to the team: ") team.append(name) player_add = input("Would you like to add a player to the list? (Yes/No)")

else: print("there are {} on the team".format(length))

2 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 42,580 Points

You are adding to the list. But you aren't updating variable length. Currently length takes the value where it is declared which is zero. Updated code below

team = []
length = 0
player_add = input("Would you like to add a player to the list? (Yes/No)")

while player_add.lower() == "yes":
    name = input("Enter the name of the player you would like to add to the team:")
    team.append(name)
    player_add = input("Would you like to add a player to the list? (Yes/No)")

else:
    length = len(team)
    print("there are {} on the team".format(length))
Shane Cookson
Shane Cookson
778 Points

Ah, I see, got it thanks mate!