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

How do I set and print player numbers?

Been at this for a while now and find myself confused about how to do this.

Snapshot: https://w.trhou.se/ydt7x617uy

What I have now I think works to the point where it's possible to exit the loop. However, I have no idea how to access the player numbers. What I'd like it to print out is this:

There are x number of players: Player 1: name Player 2: name Player 3: name

I think I could write out all those labels and assign them to the objects in the list, but that seems cumbersome. I would like it to automatically assign them. Is there a way to avoid writing out all those labels? Do I need another empty list with integer values? If so, how can I mesh values from separate lists in a print statement?

Thanks in advance!

2 Answers

Josh Keenan
Josh Keenan
20,315 Points

You can use a for loop to do this task:

count = 1
for i in players:
    print("Player{}: {}".format(count, i))
    count +=1

Adding this will iterate over the players in the array, with i being the value in the array, so the player name you want, the count provides the number for the player. The format method fills the spaces set with the curly braces. Hope this helps

Wow I just learned so much! Thank you Josh Keenan ! So a for statement allows the program to go back through the inputs, counts the entries, and then it prints however many it found. Also learned that I can have formatted entries in multiple places. That was something I tried previous but didn't realize all I needed was a comma in the parens to make it work.

Josh Keenan
Josh Keenan
20,315 Points

A for loop can be used to go over a set of data you have in your program and you'll learn more about them as you progress, keep going you got this!

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

@Josh Keenan, can you please explain the for loop you have inserted above, as I am struggling to read it. Original person that made this thread, well I looked at their code... Line 4 Is 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?

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Josh Keenan here's my code, none of these properly exit :

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))               


# 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'


# TODO print the number of players on the team


# 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


# TODO Print the goal keeper's name
# Remember that lists use a zero based index ```
mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Hi Josh Keenan, this code does not exit properly either, the next step would to assign numbers to the list and select the goalie by the assigned number.

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)

    elif add_players.lower() == "n":

        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

Hi Josh Keenan the other thing is both of these while statement (in both codes I have inserted) of less then or equal the 3, allow me to enter more then 3 player names. I can't explain to myself why it allows to do so.

while len(players_name) <=3:

while len(players_name) <=3:

for this code program lets you define 4 th player cos it ll stop when player count is 4. we can use this as

" while len(players_name) < 3: "