Python Object-Oriented Python Inheritance Instances

Alex Golden
Alex Golden
3,503 Points

For Combiner Question in OOP section of Python Track: The following code yields the correct answer, but won't take?

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

def combiner(list): wrds = "" nums = 0

for item in stuff:

    if isinstance(item, (int, float)):
        nums += float(item)

    elif isinstance(item, str):
        wrds += item        

return wrds + str(nums)

print(combiner(stuff))

instances.py
def combiner(list)
    werds = ""
    numers = 0  
    for item in stuff:

        if isinstance(item, (int, float)):
            numers += float(item)

        elif isinstance(item, str):
            werds += item

       return werds + str(numers)

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

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,961 Points

Looking at the code on the bottom I see a few issues:

  • There should be a colon after def combiner(list)
  • You pass in list but then iterate over stuff. In doing so you will always return "appledog13.2" regardless of what is passed in. It should be for item in list:
  • return statement should be outdented so it lines up with for
Alex Golden
Alex Golden
3,503 Points

Thank you Kris! The code worked fine in the workspace and I couldn't figure out why it was failing the code challenge. Onward!