Python Challenge Solution

Tim sun
Tim sun
Python Web Development Techdegree Student 379 Points

is this coorect? I think i took it the wrong way (but shows the same result)

# TODO Create an empty list to maintain the player names
players = []

# TODO Ask the user if they'd like to add players to the list.
# If the user answers "Yes", let them type in a name and add it to the list.
# If the user answers "No", print out the team 'roster'
while True:
    entry = input("Would you like to add player (yes/no)  ")
    if entry == "yes":
        player_name = input("Name  ")
    elif  entry == "no":
        if len(players) >= 1:
            number = 1
            print("Your Team: ")
            for player in players:
                print("player {}: {}".format(number, player))
                number += 1

if len(players) >= 1:
        # TODO print the number of players on the team
    print("your team has {} players".format(len(players)))

    # TODO Print the player number and the player name
    # The player number should start at the number one

    # TODO Select a goalkeeper from the above roster
    goalkeeper = int(input("Who will be your goal keeper? (1-{})  ".format(len(players))))
    # TODO Print the goal keeper's name
    # Remember that lists use a zero based index
    print("Your goal keeper will be {}".format(players[goalkeeper-1]))

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

Hi there, Tim sun ! I took your code for a spin and it works just like it does in the video so great job! Remember, the primary goal of any piece of code is that it is functional which yours is. Your code may not match the teacher's letter for letter and that's ok!

If you'll notice there is a small bug in your code, but it's also there in the teacher's as well. In the case where it asks for the person's number to make goalkeeper, if you then type in a string like "Hello", the program crashes with a ValueError. You are on a journey. You will learn how to handle that but you just haven't gotten to that step yet.

But if you continue on the path you're on, you will learn heaps. Keep going! You're doing terrific thus far :sparkles: