0

I'm trying to list possibilities in a sudoku grid if another part of the program cannot solve and for some reason keep getting

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

even though the item does not use ands and the item in question is in fact asking is a single number != 0

The board pulls up a grid of 9x9 numbers and makes a solvability field for each one relying on boolean to see if there is only one solution.

def solve_sudoku(board):
    pos = ones((9,9,9), dtype= bool)
        for col in range (9):
            for row in range(9):
                   # iteration += 1
                val = board[row, col]
                if val != 0:
                    pos[row, col, :] = False
                    pos[row, :, val-1] = False
                    pos[:,col,val-1] = False
                    pos[((row/3)*3):((row/3)*3+3),((col/3)*3):((col/3)*3+3),val-1] = False
        for col in range(9):
            for row in range(9):
                if sum(pos[row,col,:]) == 1:
                    board[row, col] = list(pos[row, col, :]).index(True)+1

        for y in range(10):
            plot([-.05,9.05],[y,y],color='black', linewidth=1)
        for y in range(0, 10, 3):
            plot([-.05,9.05],[y,y],color='black', linewidth=3)

        for x in range(10):
            plot([x,x],[-.05,9.05],color='black', linewidth=1)
        for x in range(0, 10, 3):
            plot([x,x],[-.05,9.05],color='black', linewidth=3)
            axis('image')        
            axis('off')
        for x in range(9):
            for y in range(9):
                number = board[y,x]
                if number != 0:
                    plot(x+.5,9-(y+.5),marker='$'+str(number)+'$',markersize=15,color='black')
                else:
                    xpos = 0
                    for x in list(pos[row,col:]):
                        xpos += 1
                        xlist = []
                        if x.all() == True:
                            xlist.append[xpos]
                            plot(x+.5,9-(y+.5),xlist,markersize=5,color='purple')

And error

30         for y in range(9):
     31             number = board[y,x]
---> 32             if number != False:
     33                 plot(x+.5,9-(y+.5),marker='$'+str(number)+'$',markersize=15,color='black')
     34             else:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Krilion
  • 11
  • 2
  • Try to `print number` right before checking whether it is `!=False`. You'll probably find that it's an array. Check `board`'s shape too: `print board.shape` and maybe it is more than 2d. BTW, saying `if n != False` is equivalent to simply `if n:` – askewchan Oct 15 '13 at 21:45
  • Printed, small sample of shape is () (0,) (0,) (0,) (0,) (0,) (0,) (0,) (0,) () – Krilion Oct 15 '13 at 21:51
  • number.shape. Board shape was (1,) (1,) (1,) (1,) (1,) (1,) (1,) (1,). I tried slicing just the beginning and get IndexError: invalid index to scalar variable. – Krilion Oct 15 '13 at 21:55
  • So there's a problem earlier, since I would expect to get `(9,9)` for `board.shape`, and `number` shouldn't have a shape (should just be a float). – askewchan Oct 15 '13 at 21:56
  • Each half of the code worked fine independently. I do redefine board above but I redefined a part. using type() I got that it is a int the first try but then becomes an array. – Krilion Oct 15 '13 at 22:00
  • Above, when it puts the new value in, it is all int afterwards. – Krilion Oct 15 '13 at 22:01
  • Unfortunately, the only way to track these kinds of problems is to keep checking (by printing as you're doing) the type, shape, values, etc, at each line and find out what's changing them. – askewchan Oct 15 '13 at 22:04
  • By using .all on the end and chaning around a few things it parses past, now I have an actual issue! Thanks for helping me through it! – Krilion Oct 15 '13 at 22:08
  • Cool, but be careful with just using `all` on the array, because it hides the fact that you really just want one number in the first place. – askewchan Oct 15 '13 at 22:11

1 Answers1

0

Obviously, board[y,x] does not contain a single number, but rather an array.

zmbq
  • 38,013
  • 14
  • 101
  • 171