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 trialMuhammad khan
Python Development Techdegree Student 1,836 PointsList not printing
Can someone help with the code below. Whenever the user selects "N", I am trying to print the list of players but the code seems to skip that part. Not exactly sure why. I believe the while loop ends whenever "N" is selected so it should print before continuing onto the next code. Thanks for the help!
<p> # TODO Create an empty list to maintain the player names
player = []
# TODO Ask the user if they'd like to add players to the list.
ask = input("would you like to add players? \n[Y/N]")
# If the user answers "Yes", let them type in a name and add it to the list.
while ask.lower() != "n":
player_add = input("Enter is the player's name: ")
player.append(player_add)
ask = input("Enter more player's name: \n[Y/N] ")
# If the user answers "No", print out the team 'roster'
for x in range(len(player)):
player[x]
# TODO print the number of players on the team
print("No of players on the team is {}".format(len(player)))
# TODO Print the player number and the player name
# The player number should start at the number one
player_num = 1
for players in player:
print("Player {}:{}".format(player_num,players))
player_num += 1
# TODO Select a goalkeeper from the above roster
goal = input("Select the goalkeeper using number \n(1-{}): ".format(len(player)))
goal = int(goal)
# TODO Print the goal keeper's name
# Remember that lists use a zero based index
print("The goal keeper's name is: {}".format(player[goal-1])) </p>
2 Answers
Steven Parker
231,269 PointsI found two issues with this code:
- there are stray HTML tags at the beginning and end of the file
- the indentation on line 16 is excessive (two stops instead of just one)
But after fixing the issues, the loop did end and the list was printed when I selected "n".
Muhammad khan
Python Development Techdegree Student 1,836 PointsHey Steven, yeah I was just trying to see how to print the players in another way. I was able to do it using the following code both as a list and in a string. Thanks for your help!
for x in range(len(player)-1):
player[x]
print(player)
print(", ".join(player))
Steven Parker
231,269 PointsBe aware that this line still doesn't do anything lasting or visible:
player[x]
Muhammad khan
Python Development Techdegree Student 1,836 PointsMuhammad khan
Python Development Techdegree Student 1,836 PointsThanks. I fixed those. Unfortunately I am still not able to run this part of the code:
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThat's not the part where the players are printed, the printing is done in the "
for players in player:
" loop several lines later.That particular loop is very odd, it indexes through the list but does not do anything with the items (in particular, it does not "print" them). Did you want to print them twice?