Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKen Yee
1,084 PointsNeed help with challenge 1
for i in range(len(musical_groups)):
for j in range(len(musical_groups[i])):
print(musical_groups[i][j], end=',')
print()
This is my code but getting this error.
=====================================================================
FAIL: test_expected_code (__main__.TestLoopAndJoinCode)
----------------------------------------------------------------------
Traceback (most recent call last):
File "", line 32, in test_expected_code
AssertionError: 'join' not found in 'musical_groups = [\n ["Ad Rock", "MCA", "Mike D."],\n ["John Lennon", "Paul McCartney", "Ringo Starr", "George Harrison"],\n ["Salt", "Peppa", "Spinderella"],\n ["Rivers Cuomo", "Patrick Wilson", "Brian Bell", "Scott Shriner"],\n ["Chuck D.", "Flavor Flav", "Professor Griff", "Khari Winn", "DJ Lord"],\n ["Axl Rose", "Slash", "Duff McKagan", "Steven Adler"],\n ["Run", "DMC", "Jam Master Jay"],\n]\n# Your code here\nfor i in range(len(musical_groups)):\n for j in range(len(musical_groups[i])):\n print(musical_groups[i][j], end=\',\')\n print()' : For each group, you should use the join method on a separator to combine the group members
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
3 Answers
Ray Karyshyn
13,443 PointsCheck out the join function:
- http://www.pythonforbeginners.com/data-types/python-join-examples)
- https://stackoverflow.com/a/1876206
for i in range(len(musical_groups)): # Loop through each item in 'musical_groups'
print(", ".join(musical_groups[i])) # Join the sub-list using the ", " string
Ken Yee
1,084 PointsThanks Ray. Still cant figure it out. Can you provide the answers?
Myers Carpenter
6,421 PointsKen Yee We try to help without giving the exact answer here. What is your current attempt at a solution?
Ted Musgrove
4,400 PointsTed Musgrove
4,400 PointsRay can you example this to me
Ray Karyshyn
13,443 PointsRay Karyshyn
13,443 PointsIf you have a list:
my_list = ["tic", "tac", "toe"]
You can create a string with a custom selection to place between the items. In this case, I will create the word "tic-tac-toe" using
my_list
and thejoin
method.my_string = "-".join(my_list) # returns "tic-tac-toe"
As you can see, I write the selection I want in between the items ("-"), then a dot, then the method
join()
, passing the list within the parenthesis.