0

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=[]
  • Just a heads up, checking whether rowsums equal 45 is not really sufficient. You'd also have to check column sums and 3x3 block sums, and whether the values in each were unique and in [1..9]). – jedwards Jun 30 '18 at 07:17
  • @jedwards Yep, youre right. This was only part of my code and I was displaying. I had actually already implemented those parts in my code and I am stuck in finding the 3x3 sums. I made another post about it if you would like to check out. https://stackoverflow.com/questions/51114445/trying-to-figure-out-a-way-to-compare-numbers-in-a-9x9-matrix-by-using-3x3-matri –  Jun 30 '18 at 11:47

2 Answers2

1

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]).

0

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=[]
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
  • I see, so instead of using a list just add up the numbers. It works, I'll accept the answer once the 10 min timer runs out. Thanks for the help! –  Jun 30 '18 at 06:41
  • Welcome :) Also you're adding the value in `numHolder+=grid[b][x]`,So is the next line needed here?I guess `numHolder` is an `int` variable here. – Taohidul Islam Jun 30 '18 at 06:43
  • `numHolder` is a list (at least it is on the last line), `list + int` isn't defined.... – jedwards Jun 30 '18 at 06:44