0

Python developer course, working with lists, relay race practice

I’m completing the Python developer course, and I’m in the section working with lists, in the problem relay race practice. I can’t spot the bug in my code. Here’s my code: players = ["Alice","Bob","Charlie","David","Eve","Frank"] #Create 3 lists with 2 players each #Use slicing to create a list for Group 1 g1 = players[0:2] #Use slicing to create a list for Group 2 g2 = players[2:4] #Use slicing to create a list for Group 3 g3 = players[4:6] print("Group 1:",g1) #display the 1st group print("Group 2:",g2) #display the 2nd group print("Group 3:",g3) #display the 3rd group Here’s my output: Group 1: [‘Alice’ , ‘Bob’] Group 2: [‘Charlie’ , ‘David’] Group 3: [‘Eve’ , ‘Frank’] Here’s what Sololearn says is the expected solution: Group 1: [‘Alice’ , ‘Bob’] Group 2: [‘Charlie’ , ‘David’] Group 3: [‘Eve’ , ‘Frank’] I can’t move on because it continues to tell me this is wrong.

21st Oct 2023, 7:15 PM
Kate
4 Antworten
+ 7
Kate , in your post you maybe mixed up the output from your code with the *required output*. this should be: Group 1: ['Alice', 'Bob'] Group 2: ['Charlie', 'David'] Group 3: ['Eve', 'Frank'] > to get this output, we can use an additional print() statement for each group like: ... print("Group 1:") print(g1) ... 2 more advanced ways could be: use one print() statement for both required lines by using an f-string. to get the line break use \n or use one print() statement for both required lines by using comma separated arguments, with additional sep='\n' argument.
21st Oct 2023, 8:26 PM
Lothar
Lothar - avatar
+ 4
Manan Saxena , the code sample you mentioned will not pass the test cases and is creating a syntax error. the reason is: > when printing the *new-line sequence*, this has to be in quotes. but even if you put it in quotes, an other issue will raise: > when using the print() statement with multiple arguments that are separated by a comma, a space will be insertd between the arguments. this is done by default from python. the result will be: (second line of the output starts with a *space*, which does not match the required result) Group 1: ['Alice', 'Bob'] should be: Group 1: ['Alice', 'Bob']
22nd Oct 2023, 3:23 PM
Lothar
Lothar - avatar
+ 3
Lothar, by f-string do you mean the formatted string f"..."? If so then for then it is not necessary for the newline character, it can be used in all lines.
21st Oct 2023, 11:06 PM
Solo
Solo - avatar
0
use \n after after print("Group 3:" ,\ng3) like this so it will display the names in nextine do the same thing in every group
22nd Oct 2023, 1:27 PM
Manan Saxena
Manan Saxena - avatar