Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,822 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,822 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]