如何返回false和有问题的行(python数独检查器)

时间:2014-06-23 23:26:02

标签: python python-3.x sudoku

我在返回False和def checkit函数中的追加号时遇到问题。我不确定我是否错误地编写了checkrow函数。当给出正确解决的数独谜题时,它能够返回true,但是当我们要调用不正确的数独谜题时,它将返回False,并提供有问题的行。目前,它只返回有问题的行,也就是说拼图失败的位置,但不包括错误。关于如何制作它以使它返回两者的任何想法?

import sys
from scanner import *

def createList(size):
    if size == 0: return []
    else:
        return [0] + createList(size -1)
def printGrid(gridlist):
    for row in gridlist:
       print (str(row)+"\n")
def rows(g):
    return len(g)
def cols(g):
    return len(g[0])

def printMatrix(g):
    for i in range(0,rows(g),1):
        for j in range(0,cols(g),1):
            print(g[i][j],end=' ')
        print('')
    print('')

def readinput(filename,grid):
    s = Scanner(filename)
    r = s.readtoken()
    while r != "":
        r = int(r)
        c = s.readint()
        v = s.readint()
        grid[r][c]=v
        r = s.readtoken()

def checkRows(g):
    for rows in g:
        numbersInRow = []
        for number in rows:
            if number != 0 and number in numbersInRow:
                return g.index(rows)
            else:
               numbersInRow.append(number)
    return True
def checkIt(g):
    checkRows(g)
    rowSuccess = checkRows(g)
    print(rowSuccess)
def main():
    grid = createList(9)
    for i in range(9):
        grid[i] = createList(9)
    readinput(sys.argv[1],grid)
    printMatrix(grid)
    checkIt(grid)
main()

2 个答案:

答案 0 :(得分:0)

您可以退回两者:

def checkRows(g):
    for rows in g:
        numbersInRow = []
        for number in rows:
            if number != 0 and number in numbersInRow:
                return g.index(rows), False
            else:
               numbersInRow.append(number)

阅读它:

problematic_row, result = checkRows(g)

答案 1 :(得分:0)

return False, g.index(rows)将返回两个值作为元组。