Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,862 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