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)
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:
...
+ 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='')
+ 1
why do you say it's incorrect?
It's a good code.
+ 1
There's nothing wrong with the code from what I've seen
Why are u saying something is incorrect?
+ 1
the code is functioning as intended and there are no errors.
+ 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...
+ 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)
+ 1
The output would be like this
Group 1:
['Alice', 'Bob']
Group 2:
['Charlie', 'David']
Group 3:
['Eve', 'Frank']
+ 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}")
0
So looks like there was an issue in the tool itself.Thank you all and confirming that my code was correct.