数独中未定义全局名称检查行

2024-06-16 10:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我的测试有一些问题。我不知道为什么我总是一个接一个地出错。我不熟悉使用课堂

正在测试的文件

class Sudoku_Checker:
  def __init__(self,board):
    self.board = board

  def board_validater(self):
    checkRows(self.board)
    checkCols(self.board)
    checkSquares(self.board)

    return checkRows() == True and checkCols() == True and checkSquares() == True

  def checkRows(self):
      # compare = [1,2,3,4,5,6,7,8,9]
      # for i in self.board:
      #     if i.sort() == compare:
      #         continue
      #     else:
      #         return False
      return True

  def checkCols(self):
      return False

  def checkSquares(self):
      return True
# s = Sudoku_Checker()
# s.board_validater([
#   [5, 3, 4, 6, 7, 8, 9, 1, 2],
#   [6, 7, 2, 1, 9, 0, 3, 4, 8],
#   [1, 0, 0, 3, 4, 2, 5, 6, 0],
#   [8, 5, 9, 7, 6, 1, 0, 2, 0],
#   [4, 2, 6, 8, 5, 3, 7, 9, 1],
#   [7, 1, 3, 9, 2, 4, 8, 5, 6],
#   [9, 0, 1, 5, 3, 7, 2, 1, 4],
#   [2, 8, 7, 4, 1, 9, 6, 3, 5],
#   [3, 0, 0, 4, 8, 1, 1, 7, 9]
# ])

这是测试文件

import unittest
from ValidSudoku import *

class TestSum(unittest.TestCase):
    def testwillWork(self):
        """
        Check to return True
        """
        grid = [  [5, 3, 4, 6, 7, 8, 9, 1, 2],
          [6, 7, 2, 1, 9, 5, 3, 4, 8],
          [1, 9, 8, 3, 4, 2, 5, 6, 7],
          [8, 5, 9, 7, 6, 1, 4, 2, 3],
          [4, 2, 6, 8, 5, 3, 7, 9, 1],
          [7, 1, 3, 9, 2, 4, 8, 5, 6],
          [9, 6, 1, 5, 3, 7, 2, 8, 4],
          [2, 8, 7, 4, 1, 9, 6, 3, 5],
          [3, 4, 5, 2, 8, 6, 1, 7, 9]]
        checker_for_only_this_grid = Sudoku_Checker(grid)
        self.assertTrue(checker_for_only_this_grid.board_validater())

if __name__ == '__main__':
    unittest.main()

请给我一些提示谢谢。我不确定我是否以正确的方式组织代码。我只想在开始编写代码之前编写基本测试


Tags: 文件selfboardtrueforreturndefchecker
1条回答
网友
1楼 · 发布于 2024-06-16 10:31:35

将类中的函数定义为成员函数时,例如

class blah:
    ...

    def myfunc(self, args):
        pass

    def newfunc(self):
        # self is how other members of the instance are passed in, hence
        self.myfunc(args) # You must invoke it this way inside your class.

在课堂之外:

bla = blah() # instance of your class.
bla.myfunc(args) # your function called.

相关问题 更多 >