James Luberisse
James Luberisse
4,999 Points

Is it my for loop or my elif and if statements.

This is the question

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.
def combinder(list):
    for x in list:
        if x = isinstance(x, number):
            numlist += x
        elif x = isinstace(x, str):
            strlist += x
    newstring = str(strlist, numlist)

list = ["apple", 5.2, "dog", 8]


1 Answer

A few things:

  1. Your function is misspelled it should be named combiner
  2. You need need to set/declare your variables before using them in your for loop/if statements
  3. You do not need to set the isinstance() method to x
  4. Number is not a class, in this example you will have to figure out if the code is in the int, float, or str class
  5. Your second isinstance() method is misspelled, but you should make it an else statement instead
  6. You will need to return the String that they are asking you to
  7. You do not need to set what the list equals or even call the method

Hope this helps, happy coding!

def combiner(list):
    strlist = ''
    numlist = 0
    for x in list:
        if isinstance(x, (int,float)):
            numlist += x
            strlist += x
    return "{}{}".format("".join(strlist), numlist)