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

list indexing allowing user to pull data from list

So I need help allowing the user to pull information from list to allow a player to be goal keeper. I just can't seem to find the way to access the information.. Any hints would be nice..

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)
    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("{} is player nunmber {}.".format(player, count))
    count += 1 


# TODO Select a goalkeeper from the above roster

print("Please select a goal keeper from your team. Choose player 1 - {}.".format(len(da_roster)))

goal_keeper = input()


print("Your goal keeper for this game will be...") 


# TODO Print the goal keeper's name
# Remember that lists use a zero based index

[MOD: added ```python formatting -cf]

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) 
    elif 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("{} is player nunmber {}.".format(player, count)) 
    count += 1

Please take a look at this link on how you can better format your post. https://teamtreehouse.com/community/howto-guide-markdown-within-posts The above is not the solution but an illustration of how your code post would look like when formatted.

From what you have described, please confirm this. You would want a user to add players to the roster and then also allow the user to select the player who will be the goal keeper? Do you want to go through just adding players to the roster and then when you are done, you get out of the loop and pick a player to be the goal keeper?

yes that is exactly what i want to happen

1 Answer

Here is what I have added;

# TODO Select a goalkeeper from the above roster

print("Please select a goal keeper from your team. Choose player 1 - {}.".format(len(da_roster)))

#convert input to int
goal_keeper = int(input()) - 1
print("Your goal keeper for this game will be {}".format(da_roster[goal_keeper]))

Items in a list will start at index = 0, however, in your application, the user has to choose a player starting from 1, hence the need to subtract 1 from the user input, so that we can correctly access the required player. In this case, the goal_keeper value is holding the index of the goalkeeper in the da_roster list.

You never can be too sure the user will provide the correct inputs, so when you get to it, I would suggest you add error handling.