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

Karrar Al Arbo
Karrar Al Arbo
559 Points

practice creating and indexing list

i solved the practice task but i dont know if my way of thinking is correct i mean regardless of wt if the user enter something else than yes or no

i would love to see how others solved it - to open my eyes at things i might no ve seen

players = []                                                                    # empty list to maintain the player names
add_player = input("do you want to add a player ? yes / no : ")                 # Ask the user if they'd like to add players to the list.
add_another=add_player
if add_player == "yes":                                                         # If the user answers "Yes", let them type in a name and add it to the list.
    player = players.append(input("Enter player names : "))

    add_another = input("do you want to add another player ? yes / no : ")

    while add_another == "yes":

        player = players.append(input("Enter player names : "))

        add_another = input("do you want to add another player ? yes / no : ")

if add_another == "no":                                                              # If the user answers "No", print out the team 'roster'

        print(f"we have {len(players)} players")   
        n = 0                                                                        # print the number of players on the team
        for name in players:                                                         # loop - Print the player number and the player name
            n += 1
            print(f"player {n} name: {name}")                                        # The player number should start at the number one


        gatekeeper = input("who is the gatekeeper as player number? : ")             # Ask the user to select a gatekeeper from the list of players
        gatekeeper = players[int(gatekeeper) - 1]                                   
        print(f"the gatekeeper is {gatekeeper}")                                           # Print the name of the gatekeeper

        print("Thank you for playing!")     

1 Answer

Steven Parker
Steven Parker
244,139 Points

Your code is fine as is, but I understand you want to handle other answers also. Here's one way to do it:

add_player = input("do you want to add a player ? yes / no : ")              # Ask the user if they'd like to add players to the list.
while add_player != "no":
    if add_player == "yes":                                                  # If the user answers "yes", let them type in a name and add it to the list.
        player = players.append(input("Enter player name : "))
    else:                                                                    # If the user answers something else, try again
        print("try again, and please answer only \"yes\" or \"no\"")
    add_player = input("do you want to add another player ? yes / no : ")

# the loop ends when the user answered "no"
print(f"we have {len(players)} players")