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 trialAngela Anders
1,844 PointsStopping the input("Please enter the name of the player that you would like to add to the soccer team: "
I ran my program, but I can't figure out how to stop the while loop that I set up which asks the question: Please enter the name of the player that you would like to add to soccer team:
I'm hoping this is an easy fix.
Also is there a way to do a screen shot of my Workspaces? Here is what I have in the program team.py:
# TODO Create an empty list to maintain the player names
player_names = []
# TODO Ask the user if they'd like to add players to the list.
add_players = input("Would you like to add players to your soccer team? yes/no ")
# If the user answers "Yes", let them type in a name and add it to the list.
while(add_players.lower() == "yes"):
input("Please enter the name of the player that you would like the add to the soccer team: ")
player_names.append(add_players)
# If the user answers "No", print out the team 'roster'
if(add_players.lower() == "no"):
print(player_names)
# TODO print the number of players on the team
# TODO Print the player number and the player name
print(len(player_names))
# The player number should start at the number one
# TODO Select a goalkeeper from the above roster
goal_keeper = input("Who would you like to be the goalkeeper? ")
# TODO Print the goal keeper's name
print(goal_keeper)
# Remember that lists use a zero based index
And this is what I have in the REPL when I run it:
treehouse:~/workspace$ python team.py
Would you like to add players to your soccer team? yes/no yes
Please enter the name of the player that you would like the add to the soccer team: Angie
Please enter the name of the player that you would like the add to the soccer team: Owen
Please enter the name of the player that you would like the add to the soccer team: Eli
Please enter the name of the player that you would like the add to the soccer team: Izzyh
[MOD: added ```python formatting -cf]
3 Answers
Connor Walker
17,985 PointsTo snapshot the workspace you can click the little rectangle icon on the top right of your workspace, also to format your code into a more readable way when posting on the forum you can do
``` python
YOUR CODE HERE
``` (these are back ticks not apostrophes)
This will result in this (I'm not too familiar with python so the formatting might not be correct):
#TODO Create an empty list to maintain the player names
player_names = []
#TODO Ask the user if they'd like to add players to the list.
add_players = input("Would you like to add players to your soccer team? yes/no ")
#If the user answers "Yes", let them type in a name and add it to the list.
while(add_players.lower() == "yes"):
input("Please enter the name of the player that you would like the add to the soccer team: ")
player_names.append(add_players)
#If the user answers "No", print out the team 'roster'
if(add_players.lower() == "no"):
print(player_names) # TODO print the number of players on the team
#TODO Print the player number and the player name
print(len(player_names))
#The player number should start at the number one
#TODO Select a goalkeeper from the above roster
goal_keeper = input("Who would you like to be the goalkeeper? ")
#TODO Print the goal keeper's name
print(goal_keeper)
Also for your infinite loop problem, add_players will always have the same value because you are only asking it once (before the loop), if you add the
add_players = input("Would you like to add players to your soccer team? yes/no ")
line after appending the name then you would have an exit condition.
There are other ways you could add an exit condition but that is just one example, hope this helps
Ciarah Bishop
2,457 PointsI used else: to stop my loop
mohan Abdul
Courses Plus Student 1,453 PointsCiarah Bishop, my loop does not exit, even I use the else the function.
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 ```
can you please help me.
mohan Abdul
Courses Plus Student 1,453 Pointshi Ciarah Bishop, if I use the elif statement it works to a certain to point of appointing a goalie, then the loop just continues it doesn't exit the loop.
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)) ```
Chris Freeman
Treehouse Moderator 68,441 Pointsmohan Abdul, your question would get more responses if asked as a new question on the forum. Extending another question with a new question doesn’t get noticed other than the person tagged.
Chris Freeman
Treehouse Moderator 68,441 PointsWhen I run your code, the while loop successfully exits if I enter a fourth name. This causes another error: goalie
not defined:
would you like to add players to the list? Y/N Y
please enter players name? bob
would you like to add players to the list? Y/N Y
please enter players name? jan
would you like to add players to the list? Y/N Y
please enter players name? greg
would you like to add players to the list? Y/N Y
please enter players name? tim
Traceback (most recent call last):
File “<string>”, line 17, in <module>
NameError: name ‘goalie’ is not defined
mohan Abdul
Courses Plus Student 1,453 PointsChris Freeman It does not allow to post a thread on this subject, which code exits? the one with the elif or else?