Oops, try again. Your code looks a bit off. Check the console window for errors!


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096


Oops, try again. Your code looks a bit off. Check the console window for errors!
Traceback (most recent call last):
File "python", line 12, in
File "python", line 7, in flatten
AttributeError: 'str' object has no attribute 'append'

I don't know what's wrong. Can you check my code.


n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = ""
    for numbers in lists:
        for item in numbers: 
            results.append(item)
        return results



print flatten(n)


#2

.append() is a function for lists, since you are using a string to need to use +=. So results.append(item) would become results += item.


#3

I tried results += item and it didn't work.


#4

Yeah in looking over this again += is not the way to go because we are dealing with ints, but when we have result = "" it's a string.

It would be better to go back to results.append(item) and set results to equal an empty array.

Also, return results it indented too far, it's currently part of the first for loop.


#5

I get the message AttributeError: 'str' object has no attribute 'append'.


#6

Could you post what your code looks like now? Did you change results = "" to results = []?


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.