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

Ashley Keeling
Ashley Keeling
11,476 Points

how do you make instances in a function ?

does this have to be a class? do you have to split the list up and then add it together ?

instances.py
def combiner(random):
def combiner(list):
    num = 0
    str = []
    for item in list: 
        if isinstance(item, int):
           num += item
        elif isinstance(item, float):
            num += item
        else: 
            str.append(item)

    return "{}{}".format("".join(str), num)

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

James' code will pass the challenge, but you have to be careful with naming a list "list" and a string "str" because it can cause problems in some python compilers, so I offer a slightly alternate version of James' code.

def combiner(mylist):
    mystrings = []
    mysum = 0
    for item in mylist:
        if isinstance(item,int) or isinstance(item,float):
            mysum += item
        elif isinstance(item,str):
            mystrings.append(item)
        else:
            raise ValueError # in case an item comes through that is neither number nor string

    return "{}{}".format("".join(mystrings), mysum)