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 Python Collections (2016, retired 2019) Tuples Combo

I want some help for this challenge

Ok, so for this challenge I try to make a mix between to iterables. I tried many things but this code is the most logical for me to output something like the example. The problem now is when I try this code on my computer it appears that the index is out of range. I don't know the reason for this error. The code appears logical to me.

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(nums,strings):
    new_list= []
    index = 0 
    for num in nums:
        for string in strings:
            if string[index] == string:
                new_list.append((num,string))
        index = index + 1   
    return new_list

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Yes your code is logical but as the error says index is out of range

if string[index] == string:

What is the valid index range for string?

Can you be more explicit please. In the line of code which you copied, i want a statement which is always true but i don't know the length of the string itself

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Since you do

for string in strings:

you are iterating through strings - string is a single character it can only ever be index 0 (easy to get out of range). What variable should be used with index? Does that help?

I saw my mistake after i posted the comment the mistake is that i forgot an s to string[index]