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 trialMichael Nanni
7,358 PointsHow 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
20,315 PointsYou 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
ismail çelik
1,727 Pointswhile 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: "
Michael Nanni
7,358 PointsMichael Nanni
7,358 PointsWow 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
20,315 PointsJosh Keenan
20,315 PointsA 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
Courses Plus Student 1,453 Pointsmohan 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
Courses Plus Student 1,453 Pointsmohan Abdul
Courses Plus Student 1,453 PointsJosh Keenan here's my code, none of these properly exit :
mohan Abdul
Courses Plus Student 1,453 Pointsmohan Abdul
Courses Plus Student 1,453 PointsHi 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.
mohan Abdul
Courses Plus Student 1,453 Pointsmohan Abdul
Courses Plus Student 1,453 PointsHi 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: