Skip to content
Advertisement

Operand error when using an integer in a list

I’ve been trying to work on this code for hours but keep getting to the same point every time. Basically I’m trying to add up all the values in an array. However, it keeps saying unsupported operand for integer and list. I’m confused why it is saying that because the list consists of integers. This is in python.

JavaScript

Above is the fraction of the code that I believe is the problem

JavaScript

Above is the error I keep getting

Advertisement

Answer

As @kindall and @Ben Grossmann have correctly pointed out, you’re trying to add a number to a list:

JavaScript
  • total is an integer number (total = 0)
  • hours is a list (hours = [0,0,0,0,0])

Change your code to this to make it work:

JavaScript

Side note:

There is an elegant way to get the sum of a list of numbers:

JavaScript

sum() is a built-in function (it comes with Python out-of-the-box). You can read more about it here.

Advertisement