Treehouse, list creation, challenge

IB Year 1 HL Computer Science
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
minseok123
Helper!
Posts: 9
Joined: 17 Sep 2018, 10:14

Treehouse, list creation, challenge

Post by minseok123 » 08 Oct 2018, 12:22

https://teamtreehouse.com/library/the-challenge-13

MY CODE

# TODO Create an empty list to maintain the player names

players = []

# TODO Ask the user if they'd like to add players to the list.
# 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'

answer = input("do you want to add a player to the list? [yes/no] ")
if answer == "yes":
players.append (input("enter the name of the player to add to the team: "))
answer_2 = input("would u like to add another player? [yes/no] ")

while answer_2 == "yes":
if answer == "no":
break
players.append(input("enter the name of the player to add to the team: "))
answer_2 = input("would u like to add another player? [yes/no] ")

# TODO print the number of players on the team
print("there are {} players on the team".format (len(players)))

# TODO Print the player number and the player name
print("player 1:", players[0])
print("player 2:", players[1])
print("player 3:", players[2])

I don't know how to make it so that it would print player 1, 2, 3, etc. for each added items on the list.

stuck at 1 58
1 x

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Treehouse, list creation, challenge

Post by Mr. MacKenty » 08 Oct 2018, 21:57

Hello :-)

Code: Select all

#
# Your code is below: 
#
# TODO Print the player number and the player name
#
print("player 1:", players[0])
print("player 2:", players[1])
print("player 3:", players[2])

#
# What about this: 
# 
counter = 1
for player in players:
    print("player ", counter, ": ", player)
    counter = counter + 1
minseok123 wrote:
08 Oct 2018, 12:22
https://teamtreehouse.com/library/the-challenge-13

MY CODE

# TODO Create an empty list to maintain the player names

players = []

# TODO Ask the user if they'd like to add players to the list.
# 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'

answer = input("do you want to add a player to the list? [yes/no] ")
if answer == "yes":
players.append (input("enter the name of the player to add to the team: "))
answer_2 = input("would u like to add another player? [yes/no] ")

while answer_2 == "yes":
if answer == "no":
break
players.append(input("enter the name of the player to add to the team: "))
answer_2 = input("would u like to add another player? [yes/no] ")

# TODO print the number of players on the team
print("there are {} players on the team".format (len(players)))

# TODO Print the player number and the player name
print("player 1:", players[0])
print("player 2:", players[1])
print("player 3:", players[2])

I don't know how to make it so that it would print player 1, 2, 3, etc. for each added items on the list.

stuck at 1 58
0 x

User avatar
minseok123
Helper!
Posts: 9
Joined: 17 Sep 2018, 10:14

Re: Treehouse, list creation, challenge

Post by minseok123 » 09 Oct 2018, 23:20

I did it! Here's my code

Code: Select all

# TODO Create an empty list to maintain the player names

players = []

# TODO Ask the user if they'd like to add players to the list.
# 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'

answer = input("do you want to add a player to the list? [yes/no]  ")
if answer == "yes":
    players.append (input("enter the name of the player to add to the team: "))
    answer_2 = input("would u like to add another player? [yes/no]  ")

while answer_2 == "yes":
    if answer == "no":
        break
    players.append(input("enter the name of the player to add to the team: "))
    answer_2 = input("would u like to add another player? [yes/no]  ")

# TODO print the number of players on the team
print("there are {} players on the team".format (len(players)))  

# TODO Print the player number and the player name
# The player number should start at the number one

counter = 1
for player in players:
    print("player", counter, ": ", player)
    counter = counter + 1

# TODO Select a goalkeeper from the above roster
goalkeeper_number = input("Please select the goal keeper by selecting the player number. (1-" + str(counter - 1) + ")  ")

# TODO Print the goal keeper's name
print("Great! The goalkeep for the game will be", players[int(goalkeeper_number) - 1])

# Remember that lists use a zero based index
0 x

Post Reply