Help me with this

Question:
I need help with making it print the names instead of the groups
Repl link:

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:")
#display the 1st group


print("Group 2:")
#display the 2nd group


print("Group 3:")
#display the 3rd group

Welcome to Ask! Those “groups” are called lists. Is this a school assignment by any chance?

5 Likes

Most likely what you are looking for is something along the lines of this:

print("Group 1:")
print(", ".join(g1))

str.join(list) takes every element in a list and connects them together using the str. Heres an example of how it works.

#example code
string = " is "
array = ["john", "my friend, and so", "sue."]

joined = string.join(array)

print(joined)
# should return "john is my friend, and so is sue."

Hello! If you’re trying to separate players by groups you can do this way:

  1. Use dictionary as sorting players by groups:
# Separating players into a groups
groups = {
    "group1":["Alice", "Bob"],
    "group2":["Charlie","David"],
    "group3":["Eve","Frank"]
}
  1. To use method separator.join(list) for joining list by separator:
# Separating players into a groups
groups = {
    "group1":["Alice", "Bob"],
    "group2":["Charlie","David"],
    "group3":["Eve","Frank"]
}

# Separator string
separator = ", "

# Printing the group to the console:
print("group1: ", separator.join(groups["group1"]), sep="", end=".")

# Outputs: "group1: Alice, Bob."

Well, I think you need to do some changes with your print command, you can try below code to fix this error.

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:")
# Display the names of players in the 1st group
for player in g1:
    print(player)

print("Group 2:")
# Display the names of players in the 2nd group
for player in g2:
    print(player)

print("Group 3:")
# Display the names of players in the 3rd group
for player in g3:
    print(player)

Thanks

1 Like