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

instance.py code not working. i fail to see anything wrong, logic wise. Please help.

Here is my code:

def combiner(arg): strings_list = [] result = 0

for k in arg:
    if isinstance(k, str):
        strings_list.append(k) 
        strings_words = "".join(strings_list)
        return string_word
    elif isinstance(k, (int, float)):
        result += k
        return result
    else:
        pass

return "{}{}".format(strings_words, str(result))

Not sure if there is an indentation error or something...

instances.py
def combiner(arg):
    strings_list = []
    result = 0

    for k in arg:
        if isinstance(k, str):
            strings_list.append(k) 
            strings_words = "".join(strings_list)
            return string_word
        elif isinstance(k, (int, float)):
            result += k
            return result
        else:
            pass

    return "{}{}".format(strings_words, str(result))

1 Answer

Hey @Ibrahim Khan

I have made some comments / suggestions in your code, this should help get this working. Hope this helps.

def combiner(arg):
    strings_list = []
    result = 0

    for k in arg:
        if isinstance(k, str):
            strings_list.append(k) 
            # Moved: strings_words = "".join(strings_list) //outside the for loop so it can exicute after the list is fully created.
            # Removed: return string_word //because return will break out of the loop so the code will not finish
        elif isinstance(k, (int, float)):
            result += k
            # Removed: return result //because return will break out of the loop so the code will not finish
        else:
            pass
    strings_words = "".join(strings_list)

    return "{}{}".format(strings_words, str(result))

Thanks @speeddemon, code seems to pass! Cheers!