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
Marquis Hurley
631 PointsData entered by user does not stay in the list. help!!
Every time a name is entered it is being overridden by the next item that comes from user next. The last name entered becomes the only name left in the list but listed repeatedly. Heres my code:
da_roster = []
while True:
add_player = input("Would you would you like to add some players the team? Yes or No? ")
if add_player.lower() == 'yes':
player_name = input("What is the name of the player? ")
da_roster.append(player_name)
print()
if add_player.lower() =='no':
break
else:
print(da_roster)
print("There are", len(da_roster), "players on the team.")
count = 1
for player in da_roster:
print(f"{player_name} is player nunmber {count}.")
count += 1
print(f"Please select a goal keeper from your team. Choose player 1 - {len(da_roster)}.")
goalie = int(input())
print(f"Your goal keeper for this game will be...{da_roster[goalie]}")
# Remember that lists use a zero based index
1 Answer
KRIS NIKOLAISEN
54,974 PointsSome comments
1) Your while condition will never be false so not sure what this code is doing
else:
print(da_roster)
2) You should use player instead of player_name here:
for player in da_roster:
print(f"{player} is player nunmber {count}.")
count += 1
player_name is the variable you used to store input and will have the value of the last assignment
3) The list starts with index 0 so to get the correct item you will need to subtract 1 here:
goalie = int(input()) - 1
Marquis Hurley
631 PointsMarquis Hurley
631 Pointsthanks that helped a bunch!