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

code not working, can't figure out why

I ran my code in workspaces, apart from saying something is off with tab and indentation, workspace doesn't say where else is wrong.

can anyone gimme a hand? thanks a mil!

instances.py
def combiner(argu):
    sum = 0
    word = ''
    for it in argu:
        if isinstance(it, (int,float)):
            sum += ''.join(it)
        elif isinstance(it, str):
            word += ''.join(it)
        return word+str(sum)

1 Answer

diogorferreira
diogorferreira
19,363 Points

When you are adding the it to either variables (sum and word), you don't need to .join() them

  • It raises an error anyways as it is not an iterable like the array they are passing to you, it would be things like the strings 'apple' 'dog' or integers like 9 or 7
  • The return statement is also inside the for loop just indent it back so it only calls after the for loop is called otherwise it would return after ever loop

These are the changes I made to your code

def combiner(argu):
    sum = 0
    word = ''
    for it in argu:
        if isinstance(it, (int,float)):
            sum += it
        elif isinstance(it, str):
            word += it

    return word+str(sum)
Jialong Zhang
Jialong Zhang
9,821 Points

You can only use join in a list, tuple, dictionary. You convert them to string, and separate by the seperator. The usage of join is something like:

example_list = ['item1','item2','item3']
print('|'.join(example_list))