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

Hi there, am having trouble with the code below and it seems quite right. Kindly help me with it.

I am to create function named combiner that takes a single argument, which will be a list made up of strings and numbers.

And it should return 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". And the instruction says, "Be sure to use isinstance to solve this as I might try to trick you."

instances.py
def combiner(mixed_list):
    mixed_string = ""

    for item in mixed_list:
        mixed_string.append(item) 

    return mixed_string

2 Answers

Kuanyshbek Ospanov
Kuanyshbek Ospanov
17,416 Points

First, create variables that will contain strings and numbers. After that use this if statement:

if isinstance(item, str):
    list_strs += item
else:
    ist_nums += item

Then combine them and return

return list_strs + str(list_nums)

Thanks

You have a good start but you are missing a few things. You should review isinstance first. You can do that here

In your for in loop you need to check if item is a string or a number using isinstance. If it is a string append it to your string variable. If it is a number add it to a sum variable. Note: you should check for both integer and float. Once complete return the combined string variable and sum variable.

Woow, this was very helpfull. Thanks Kris