Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python

Name: Anonymous 2010-05-19 5:58

<code>def add_lists(a, b):
    """
      >>> add_lists([1, 1], [1, 1])
      [2, 2]
      >>> add_lists([1, 2], [1, 4])
      [2, 6]
      >>> add_lists([1, 2, 1], [1, 4, 3])
      [2, 6, 4]
    """
    new_list = []
    i = 0
    while i < len(a):
        new_list += a(i) + b(i)
        i += 1
    return new_list

if __name__ == '__main__':
    import doctest
    doctest.testmod()</code>

TypeError: 'list' object is not callable

How can I fix this?

Name: Anonymous 2010-05-19 7:38

>>10
Thank you for the information.  I doubt it's the book.  Rather, I'm not paying close enough attention.  I believe I've figured it out. 

new_list += a(i) + b(i):
is incorrect because I was asking for the element of a tuple instead of a list.

new_list += a[i] + b[i]:
is incorrect because I was attempting to concatenate an integer to a list.  ie: "[] + int"

new_list += [a[i] + b[i]]:
corrects the above because it concatenates a list instead of an integer. 

Once again thank you for the help.  I appreciate it. 

I'll continue plucking through this book and will work at reading and debugging more carefully.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List