Python Object-Oriented Python Inheritance Instances

Peng Ren
Peng Ren
4,119 Points

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,362 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,816 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))