Python数独谜题求解器无法正确显示谜题

我试图写一个Sudoku难题求解器,到目前为止,我一直在试图让它显示难题。到目前为止,这是我的代码:


class Cell:

'''A cell for the soduku game.'''

def __init__(self):

    #This is our constructor

    self.__done = False #We are not finished at the start

    self.__answer = (1,2,3,4,5,6,7,8,9) #Here is the tuple containing all of our possibilities

    self.__setnum = 8 #This will be used later when we set the number.

def __str__(self):

    '''This formats what the cell returns.'''

    answer = 'This cell can be: '

    answer += str(self.__answer) #This pulls our answer from our tuple

    return answer

def get_possible(self):

    '''This tells us what our possibilities exist.'''

    answer = ()

    return self.__answer

def is_done(self):

    '''Does a simple check on a variable to determine if we are done.'''

    return self.__done

def remove(self, number):

    '''Removes a possibility from the possibility tuple.'''

    if number == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9: #Checks if we have a valid answer

        temp = list(self.__answer) #Here is the secret: We change the tuple to a list, which we can easily modify, and than turn it back.

        temp.remove(number)

        self.__answer = tuple(temp)

def set_number(self, number):

    '''Removes all but one possibility from the possibility tuple. Also sets "__done" to true.'''

    answer = 8

    for num in self.__answer:

        if num == number:

            answer = number #Checks if the number is in the tuple, and than sets that value as the tuple, which becomes an integer.

    self.__answer = answer

    self.__done = True

    return self.__answer


尝试打印时,会出现两条垂直的管道(|)。有人可以告诉我我在做什么错吗?


拉丁的传说
浏览 197回答 3
3回答

回首忆惘然

这是错误的(它将始终为True)if number == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9:采用if 1 <= number <= 9:这也是错的for char in self.__file:&nbsp; &nbsp; if char == '.':&nbsp; &nbsp; &nbsp; &nbsp; self.__puzzle += ' '&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; self.__puzzle += char遍历文件会产生行而不是字符。我建议您以较小的部分编写和测试代码。print在其中放一些s,以确保代码正在执行您期望的操作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python