-1

I thought sum a list of numbers in Python would answer my question but it didn't.

For context, I am working on Project Euler, Problem 9: https://projecteuler.net/problem=9

I am getting the error

TypeError: unsupported operand type(s) for +: 'int' and 'list'

for the line

while sum(triplesList) <= 1000:

I don't know why sum(listname) isn't working.

Here is all the code:

triplesList = []

a = 0
b = 0

while sum(triplesList) <= 1000:
    a += 1 
    b += 1 

    triplesList = [[a,b] for i in range(1)]
    triplesList.append( a**2 + b **2)

    if (math.sqrt(triplesList[1])).is_integer():
        triplesList[1] = int(math.sqrt(triplesList[1]))

    if sum(triplesList[0], triplesList[1]) == 1000:
        print triplesList
        print sum(triplesList[0] , triplesList[1])
        print reduce(lambda x, y:  x * y, triplesList[0], triplesList[1])

I appreciate the help!

Community
  • 1
  • 1
  • 3
    `triplesList = [[a,b] for i in range(1)]` makes a `list` of `list`, therefore you cannot sum `triplesList`. – Cory Kramer Oct 09 '14 at 16:45
  • I'm confused as to why I can use the sum as a conditional BEFORE I've made it a list of lists. – Longdaysjourneyintocode Oct 09 '14 at 17:00
  • Because you intialized it to `triplesList = []` so it was just a list. It doesn't become a list of lists until you perform the list comprehension that I commented on. – Cory Kramer Oct 09 '14 at 17:01
  • Sorry. I meant that I'm confused as to why I CAN'T use the sum as a conditional...for exactly the reason you stated. The error message comes at the while statement. – Longdaysjourneyintocode Oct 09 '14 at 17:07
  • Because what would that even mean? For example what is the sum of `[[1,2,3],[1,2,3]]`? – Cory Kramer Oct 09 '14 at 17:08
  • Pretty sure we're not understanding each other. The error message happens when the list is still empty. It happens BEFORE triplesList becomes a list of lists. – Longdaysjourneyintocode Oct 09 '14 at 17:12
  • 1
    No it doesn't. I can literally promise that `sum([])` is legal and results in `0`. The `while` condition does fail, but not until the second iteration – Cory Kramer Oct 09 '14 at 17:13

2 Answers2

1

sum() only accepts a iterable objects of numbers, not a list of lists.

triplesList is a list of lists when you do this to it:

triplesList = [[a,b] for i in range(1)]

Thus, sum() whines that it doesn't know how to add lists of lists, just lists of integers

I believe that line could instead read:

triplesList = [a, b]
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
ErlVolton
  • 6,714
  • 2
  • 15
  • 26
  • 1
    [sum](https://docs.python.org/2/library/functions.html#sum) can be used on anything iterable which contains number types valid for the '+' operator – Farmer Joe Oct 09 '14 at 17:28
1

Change this line

triplesList = [[a,b] for i in range(1)]

To

triplesList = [a, b, a**2 + b**2]

And remove the

triplesList.append( a**2 + b **2)

This will produce a 1D list of the form [a,b,c] which is what I get the feeling you are after from your question.

The former will produce a list of the form

[[a,b], c]

Which you cannot sum

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218