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

Leo Marco Corpuz
Leo Marco Corpuz
18,975 Points

Isinstance challenge

Here’s my updated code. Thanks!

instances.py
def combiner(oldList):
    newList=[]
    number=0
    for item in oldList:
        if isinstance(item,string):
            newList.append(item)
        elif isinstance(item,(float,int)):
            number+=item
    newList.append(number)
    newList.join()
return newList

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Leo,

Let's run your code and start by finding the syntax errors:

First:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/User/combiner.py", line 11
    return newList
    ^
SyntaxError: 'return' outside function

This is telling you that your return statement is indented at the wrong level.

Next:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/User/combiner.py", line 5, in combiner
    if isinstance(item,string):
NameError: name 'string' is not defined

This is telling you that you've used an invalid term string in isintance. Let's look at the type of a string to check what the type's name is:

>>> type('hello')
<class 'str'>

Next:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/User/combiner.py", line 10, in combiner
    newList.join()
AttributeError: 'list' object has no attribute 'join'

Look at the syntax for the join() method. Note that it is a method on a string, not a method on a list. Note also that the list must comprise only strings.

Once these syntax errors are fixed, this is what we get when we run the code, passing it the suggested example code from Treehouse:

>>> combiner(["apple", 5.2, "dog", 8])
['apple', 'dog', '13.2']

This is because you do not return the result of joining the list, you return the list.

Hope these hints point you in the right direction.

Cheers,

Alex