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 The Challenge

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

my loop exits on 4th name I when put '' <=3'' , and i dont know how to get the players to print according to the spec

my loop exits on 4th name I when put ''' <=3''' why does it do that? I don't know how to get the players to print according to the specifications of the challenge i.e to get the names assigned to numbers starting from 1 and then choose a player to be goalie by their assigned number. here's my code:

players_name = []
# TODO Create an empty list to maintain the player names
# TODO Ask the user if they'd like to add players to the list.
while len(players_name) <=3:

    add_players = input("would you like to add players to the list? Y/N  " )
    if add_players.lower() == "y":
        players_nom = input("please enter players name? ")
        players_name.append(players_nom)

else:

    print (*players_name, sep='\n')

    goalie = input(" please select goalie from the list")

    print("The goalie {} ".format(goalie))     ```
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Steven Parker or anyone else who is willing to help (thank you) I can't seem to read this code, its confusing.

count = 1
for i in players:
    print("Player{}: {}".format(count, i))
    count +=1 ```
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Steven Parker or anyone else who is willing to help (thank you).

and this has an input with yes and no, it is not followed up with an if statement. how does it work without an if statement? What does the break function do on line 11?:

players = []
# TODO Ask the user if they'd like to add players to the list.
add_player = str.lower(input("Would you like to add a player to the list? \n(Yes/No) "))
player_name = input("What's the player's name? ")
players.append(player_name)
# If the user answers "Yes", let them type in a name and add it to the list.
while len(players) <= 3:
    more_players = str.lower(input("Would you like to add another player? \n(Yes/No) "))
    if more_players == "no":
        break
    player_name = input("What's the player's name? ")
    players.append(player_name)
# If the user answers "No", print out the team 'roster'
# TODO print the number of players on the team
print("There are", len(players), "players on the team.")  ```

1 Answer

Steven Parker
Steven Parker
229,771 Points

Using "while len(players_name) <=3:" as the loop sets the condition that the loop should only run while the list has 3 or less items in it. So it will always stop after taking a 4th player. If that's not your intention you may need a different conditional test.

Here's your 2nd item with comments added:

count = 1                                   # start the player count with 1
for i in players:                           # let "i" represent each player name in turn
    print("Player{}: {}".format(count, i))  # print the count followed by the player's name
    count +=1                               # add 1 to the count before going to the next player

And for your 3rd item, the input is followed up by this "if" statement :point_right: if more_players == "no"
It works by only doing the "break" when the answer was "no". And a "break" causes a loop to end.

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Hi Steven Parker, my code kind of works, the only bit that doesn't work full is the roaster bit for the players. BTW if you are to test my code, it allows me to enter four names then it exits. I specified in the code for 3 or less then 3 names but when i run the code it allows me to enter 4 names. If i am to get the second bit right you mean:

player_nom = 1 
for player_name in player_nom
    print("player {}: {}".fomat(count, player_nom))
    player_nom +=1```
Steven Parker
Steven Parker
229,771 Points

Your limit of "3 or less" is for taking on another player. So when you have 3, it allows a 4th; but when you have 4 it stops. If you only want to allow 3 players, use "< 3" instead of "<= 3".

And your refactoring of the 2nd item changes the names inconsistently so it won't work. You can certainly use any names you like, but you must change each name in every location where it is referenced.

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Steven Parker or anyone else kindly willing to help. I couldn't quite get the second item right so i watched the solution video, and recreated it and line 20 is not function correctly. sometimes it prints out a random letter other time it prints out a letter in relation to the number and character position of the name. the file is team2.py. https://w.trhou.se/9s6ijgtncy

Steven Parker
Steven Parker
229,771 Points

At first glance, the code on line 14 stands out:

for players_name in players_name:

You should use a different variable for the individual player than the one used for the entire list of players.

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

thanks Steven Parker, i don't why this challenge popped up if i wasn't taught or shown something. I watched the Iteration video for lists and its all been explained.

Steven Parker
Steven Parker
229,771 Points

mohan Abdul — good deal. You can mark the question solved by choosing a "best answer".
And happy coding!