user9372262
user9372262

Reputation:

Issue adding all the elements from one row in a list and getting the sum

The problem I am getting here seems simple, but I have been searching all over for a solution in my situation and cannot find anything. Basically I am trying to see if the grid given is a valid/solve-able game of Sudoku. I believe my means of solving it is correct and I have been able to get the sum of all the numbers in a column and check if it != 45. The problem that I am getting is when I try to add every number in a row, it gives me the error:

TypeError: 'int' object is not iterable

I am confused as to why I am getting this error. I am still learning python, but I am pretty comfortable with java. The code I would use to do this in java is somewhat related, so that could be the problem. Let me know what you guys see:

for b in range(0,9):
    for x in range(0,9):
        numHolder+=grid[b][x]
        if sum(numHolder) != 45:
            return False
    numHolder=[]

Upvotes: 0

Views: 75

Answers (2)

Taohidul Islam
Taohidul Islam

Reputation: 5414

Write if numHolder != 45: not if sum(numHolder) != 45:. sum function expects a list,tuple etc but not a single value.

Update: If numHolder is a list then you should write:

for b in range(0,9):
    for x in range(0,9):
        numHolder.append(grid[b][x])
        if sum(numHolder) != 45:
            return False
    numHolder=[]

Upvotes: 0

user6152171
user6152171

Reputation:

When you use += on a list, it tries to add all the items from the list on the right of the operator into the list on the left. However, an int is not a list, so you will have to use numHolder.append(grid[b][x]).

Upvotes: 1

Related Questions