Challenge Solution2:28 with Ken Alger
One solution to the list creation challenge.
Welcome back. 0:00 How did you do? 0:01 If you weren't able to complete each task in the to-do list, that's okay. 0:02 Learning new things can be difficult, but by practicing them, you'll get better. 0:07 Okay, are you ready to see my approach? 0:12 Here we go. Open up team.py. 0:15 So our first to-do is to create an empty list to maintain the player names. 0:20 Remember that we can create a list with opening and closing hard brackets, and 0:24 since lists are mutable, we can start with an empty one and add to it as needed. 0:28 The next to do task is to take some input from the user and enter a loop. 0:33 A while loop seems like a good thing to use here. 0:38 Here we're asking if they want to add players to the list. 0:41 While they answer yes, we ask for a player's name to add to the team. 0:44 Then we use the append method to add the name to our player's list, and 0:49 ask if they want to add more. 0:53 If yes, the while loop continues. 0:55 If no, we move on to the next to-do task. 0:58 The next task is to print the number of players on the team. 1:02 Remember we use the Len method to get the number of items in our list. 1:06 We added in a bit of formatting with line breaks to add a nice touch as well. 1:10 The next task is to print the player number and the player name. 1:16 Sounds like a counter starting at the number one is an order here and 1:19 we'll need to increment that for each player in our list. 1:22 Our next task is to select our goalkeeper from the above roster. 1:26 Here, we're asking for the player number for our goalkeeper. 1:30 Again, using the Len method to help provide some context for 1:33 the user as to what to enter. 1:37 This next line, line 30, did you remember this? 1:39 Recall that taking input returns a string. 1:43 And for our indexing, we'll need that to be an integer. 1:46 All right, our next and last to-do, to print the goalkeeper's name. 1:50 Our printed out player numbers isn't using zero-based indexing, right? 1:55 Therefore when we select our goalkeeper, 2:00 our keeper variable is actually one higher than the zero-based players' list. 2:02 To print out the goalkeeper's name then, 2:08 we need to remember to subtract one from the keeper variable. 2:10 Score one for another practice session done. 2:14 Thanks for working through it with me. 2:17 Keep up the momentum in your Python learning. 2:19 With more practices like this, you'll reach your goal soon. 2:22 Until next time, happy coding. 2:25
You need to sign up for Treehouse in order to download course files.Sign up