0

What is incorrect in this 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] # Display the 1st group print("Group 1: \n",g1) # Display the 2nd group print("Group 2: \n",g2) # Display the 3rd group print("Group 3: \n",g3)

9th Jan 2025, 12:16 AM
Veena Vijay
Veena Vijay - avatar
10 Answers
+ 3
Veena Vijay , the code seems to be correct since it does not raise any exception. but the output is *not* as expected. > this is an exercise called `relay race` from the python tutorial `python developer`. # the output of the current code is: Group 1: ['Alice', 'Bob'] Group 2: ... this is *not correct*. the line with the names should start *without a space* at the beginning of the line like: Group 1: ['Alice', 'Bob'] Group 2: ...
9th Jan 2025, 8:43 PM
Lothar
Lothar - avatar
+ 3
‏‪‏‪‏‪‏‪Crazy Person‬‏‬‏‬‏ , the output of your code sample is *not as expected*. read the comments in my post. > the reason for the (unnecessary) space at the begin of the line with the names is that print inserts a space (by default) when we use a comma to separate multiple arguments. to get the output done properly, we can set the separator to an empty string like: print("Group 1:\n", g1, sep='')
10th Jan 2025, 6:50 AM
Lothar
Lothar - avatar
+ 1
why do you say it's incorrect? It's a good code.
9th Jan 2025, 5:15 AM
Bob_Li
Bob_Li - avatar
+ 1
There's nothing wrong with the code from what I've seen Why are u saying something is incorrect?
9th Jan 2025, 6:06 AM
DashFR
DashFR - avatar
+ 1
the code is functioning as intended and there are no errors.
9th Jan 2025, 6:58 AM
Mustafa Raza
+ 1
Lothar good catch. I didn't realize it was a code exercise problem. Yeah, those can be strict with spaces, spelling and capitalization. my more_itertools code also don't work there, it seems like installing modules is restricted, so I deleted it to avoid confusing people...
9th Jan 2025, 9:49 PM
Bob_Li
Bob_Li - avatar
+ 1
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] # Display the 1st group print("Group 1:\n", g1) # Display the 2nd group print("Group 2:\n", g2) # Display the 3rd group print("Group 3:\n", g3)
10th Jan 2025, 12:35 AM
‏‪‏‪‏‪‏‪‏‪‏‪‏‪أنا تمرة الاحباب حنضلة العدى‬‏‬‏‬‏‬‏
‏‪‏‪‏‪‏‪‏‪‏‪‏‪أنا تمرة الاحباب حنضلة العدى‬‏‬‏‬‏‬‏ - avatar
+ 1
The output would be like this Group 1: ['Alice', 'Bob'] Group 2: ['Charlie', 'David'] Group 3: ['Eve', 'Frank']
10th Jan 2025, 12:36 AM
‏‪‏‪‏‪‏‪‏‪‏‪‏‪أنا تمرة الاحباب حنضلة العدى‬‏‬‏‬‏‬‏
‏‪‏‪‏‪‏‪‏‪‏‪‏‪أنا تمرة الاحباب حنضلة العدى‬‏‬‏‬‏‬‏ - avatar
+ 1
Hi Veena, if you don't want a space before the group, you can do like this: # Display the 1st group print(f"Group 1:\n{g1}") # Display the 2nd group print(f"Group 2:\n{g2}") # Display the 3rd group print(f"Group 3:\n{g3}")
14th Jan 2025, 1:01 PM
Bruno LASSABE
Bruno LASSABE - avatar
0
So looks like there was an issue in the tool itself.Thank you all and confirming that my code was correct.
12th Jan 2025, 12:30 PM
Veena Vijay
Veena Vijay - avatar