Python Object-Oriented Python Inheritance Instances

Rutendo Chaurura
Rutendo Chaurura
6,361 Points

help me on this task

Alright, here's a fun task! Create a function named combiner that takes a single argument, which will be a list made up of strings and numbers. Return a single string that is a combination of all of the strings in the list and then the sum of all of the numbers. For example, with the input ["apple", 5.2, "dog", 8], combiner would return "appledog13.2". Be sure to use isinstance to solve this as I might try to trick you.

here is my code

def combiner(mylist): number = 0 string = ""

for item in mylist:
    if isinstance(item, str):
        string += item

    elif isinstance(item, (int, float)):
        number += item

    return string + str(number)
instances.py
def combiner(mylist):
    number = 0
    string = ""

    for item in mylist:
        if isinstance(item, str):
            string += item

        elif isinstance(item, (int, float)):
            number += item

        return string + str(number)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 58,893 Points

You are very close! The return is indented too far. This causes the for loop to return after the first iteration.