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

devante wallace
devante wallace
5,151 Points

My code works in the workshop but not in the question?

Please could you advise on what i need to change

instances.py
strings = []
nums = []

def combiner(*args):
    for val in args:
      if isinstance(val, str):
        strings.append(val)
      elif isinstance(val, (int, float)):
        nums.append(val)

    total = "".join(strings) + (str(sum(nums))) 

    return total

You have to initialize your variables within your function and the variables themselves can't be lists so set them as an empty string and give your int variable a 0 default value like:

strings = ""
nums = 0

the rest of your code should look something like:

    for val in args:
        if isinstance(val, str):
            strings += val
        else:
            nums += float(arg)
    return strings + str(nums)

instead of using append the variables will be seperated depending on their type and then added together by using += the return statement then joins back the separated variables to make one string or you could assign total:

total = strings + str(nums)

and return total like you did in your code

1 Answer

As a test try the example given in the challenge:

print(combiner(["apple", 5.2, "dog", 8]))

The result should be appledog13.2. The result of your function is 0.