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

Python

Combo challenge help needed

So I tried solving the challenge but no luck can someone explain me how it is done?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

I'm happy to help. Can you post what you have tried so far or provide more details on where you are stuck?

def 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]

I 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
Chris Freeman
Treehouse Moderator 68,468 Points

Apologies for the comment about posting solutions. I thought that the post was not from you (the original poster)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Your 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.

Shoud I use enumerate() inside the for loop and comibne the lists or is there a better way?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Enumerate 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.

Required 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