0

Help me please. Debug my code.

players = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"] group_1 = players[0:2] group_2 = players[2:4] group_3 = players[4:6] print("Group 1:", group_1) print("Group 2:", group_2) print("Group 3:", group_3) Output must be: group_1: ["Alice", "Bob"] Etc. My code do it in one line

16th Nov 2023, 6:29 PM
Olga Belyavskaya
Olga Belyavskaya - avatar
2 Réponses
+ 7
Ольга Белявская , your code sample: print("Group 1:", group_1) will print the output in one line, which is not requested. solutions can be: > use 2 independent lines of code. > if we want to use the code in one line, we could use something like: print("Group 1:\n", group_1) *but* by using comma notation inside the print function, python will insert 1 space at each comma posion. this space will be the first character in the second line and is not the requested solution. > we can use an f- string to achieve the correct output: print(f"Group 1:\n{group_1}")
17th Nov 2023, 7:26 AM
Lothar
Lothar - avatar
+ 4
You can add a line break by using "\n": print("A\nB") Or use the sep-parameter: print("A", "B", sep="\n")
16th Nov 2023, 6:32 PM
Lisa
Lisa - avatar