Tobiasz Lorenc
Tobiasz Lorenc
3,757 Points

Hello again, i have problem with end my code

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.

Bummer: Didn't get the expected output
def combiner(*args):

    sums = 0
    words = ""
    for arg in args:
        if isinstance(arg,str) == True: 
          words += arg
        elif isinstance(arg,float) == True:
          sums += arg
        elif isinstance(arg,int) == True:
          sums += arg

1 Answer

The input provided by the challenge is a list, not multiple arguments. For this challenge, there is no need to (and you shouldn't) unpack with *.

With your code, you expect: combiner("apple", 5.2, "dog", 8).

But what you actually get is: combiner(["apple", 5.2, "dog", 8]).

James Joseph
James Joseph
2,582 Points

What Simon said, also you could make this less lines of code by only using one elif statement:

elif isinstance(arg, (int, float)):
    sums += args