Python Object-Oriented Python Inheritance Instances

Anthony Longo
Anthony Longo
1,792 Points

I know I'm doing something wrong.

I know I'm doing something wrong, I just don't know what. when I run this in workplace I get the right answer but when I try and check it, it says Bummer: didn't get the expected answer.

instances.py
def combiner(*arg):
    num = []
    stringL = []
    strings = ""
    for item in arg:
        if isinstance(item,(int,float)):
            num.append(item)
        elif isinstance(item,str):
            stringL.append(item)
    for items in stringL:
        strings += items
    return str("{}{}".format(strings,sum(num)))

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,734 Points

The challenge passes in a single list. By using *arg as a parameter you iterate the list as a whole instead of the items within the list. As such item is not an instance of int, float or string. It is an instance of a list. Remove the * so you just have def combiner(arg):

You can test this in a workspace with a print statement

def combiner(*arg):  # try both arg and *arg
    num = []
    stringL = []
    strings = ""

    for item in arg:
        print(item)  # add this 
        if isinstance(item,(int,float)):
            num.append(item)
        elif isinstance(item,str):
            stringL.append(item)
        elif isinstance(item, list):
            print("I'm a list")
    for items in stringL:
        strings += items
    return str("{}{}".format(strings,sum(num)))

print(combiner(["apple", 5.2, "dog", 8]))
Son-Hai Nguyen
Son-Hai Nguyen
1,655 Points

Really? I found this on the internet, and I thought it's pretty the same as what we're trying to do here

def userInput(ItemA, ItemB, *args):    
    lst=[]
    lst.append(ItemA)
    lst.append(ItemB)
    for arg in args:
        lst.append(arg)

So, the challenge passed on a list technically because it was setup that way right? I mean in normal situation I'd to add in as combiner("apple", 5.2, "dog", 8). The hard brackets feel a bit over-typo to me