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 trial
Paul Mihai Gansca
6,132 PointsCombo challenge help needed
So I tried solving the challenge but no luck can someone explain me how it is done?
Paul Mihai Gansca
6,132 Pointsdef combo(text1, text2):
tup1 = None
tup2 = None
for char1 in text1:
tup1.append[char1]
for char2 in text2:
tup2.append[char2]
return '{}: {}'.format(tup1, tup2)
combo('abc', 'def')
Please assume it is correctly indented my code just got rearranged here.
[MOD: added formatting]
Paul Mihai Gansca
6,132 PointsI don't know how I can append my values from both arguments and get the desired output. I have been thinking about appending characters with for loops and then try to return the answer.
Chris Freeman
Treehouse Moderator 68,468 PointsApologies for the comment about posting solutions. I thought that the post was not from you (the original poster)
2 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsYour code raises the error "NoneType has no attribute append". The result you are looking for the challenge, should be in the format [(1, 'a'), (2, 'b'), (3, 'c')]
# One way to get two values into a tuple would be:
tup = text1[0], text2[0]
# since the arguments text1 and text2 have the same length
# you can loop using
for idx in range(len(text1))
# use idx to index into arguments to make tuple
You can combine these to create all your tuples one at a time. Inside the for-loop append each new tuple to a results list (remember to init the results list to an empty list [] before the for-loop. After the for-loop, return the results list.
Post back if you need more hints.
Paul Mihai Gansca
6,132 PointsShoud I use enumerate() inside the for loop and comibne the lists or is there a better way?
Chris Freeman
Treehouse Moderator 68,468 PointsEnumerate will work. You can get an index and one of the needed items for the tuple. Then use the index to get the other item.
This challenge mimics the built-in function zip so using that would be most efficient but makes for a trivial solution.
Ari Frankel
Courses Plus Student 5,438 PointsRequired reading: Chris's answer. Here I'll add here some more details about zip() and enumerate():
1) zip() merges two iterables in to a list of tuples, pairing the first elements of each, the second elements of each, etc.
zip('foo', 'bar') # returns [(f, b), (o, a), (o, r)]
With iterables of unequal lengths, zip() will stop at the end of one iterable, leaving trailing unpaird values in the longer iterable.
zip('foobar', 'bar') #returns [(f,b), (o,a), (o,r)]
2) enumerate() unpacks a single iterable in to an iterable of tuples with a number paired to each element. It essentially 'zips' an iterable arg with the list [1,2,3...len(arg)]
enumerate('foo') #returns [(1, f), (2, o), (3, o)]
NOTE: Because arg1 in this challenge is [1, 2, 3], using enumerate() on arg2 essentially 'zips' arg2 with arg1.
Don't take my word for it - here's a link to the documentation
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsI'm happy to help. Can you post what you have tried so far or provide more details on where you are stuck?