我在使用Python编程方面相对较新。这段代码运行良好,直到我尝试将其转换为类。 我正在为数独求解器代码创建一个类,以练习类并在面向对象的编程中涉猎。
因此,我从有类似问题的用户那里读了很多问题,大多数答案是: -首先实例化该类,然后再从该类调用函数 但它们似乎都不适合我的具体示例。
这是我的课程:
#assume sudoku is imported as a np.array, white spaces replaced by zeros
class Sudoku():
solution_number = 1
def __init__ (self, sud_arr):
self.sudo = sud_arr
#print(self.sudo)
def possible (self, y, x, num):
for i in range(9):
if self.sudo[y][i] == num:
return False
if self.sudo[i][x] == num:
return False
yy = (y//3)*3
xx = (x//3)*3
for i in range(3):
for j in range(3):
if self.sudo[yy+i][xx+j] == num:
return False
return True
def solve(self):
for i in range(9):
for j in range(9):
if self.sudo[i][j] == 0:
for nr in range(1,10):
if Sudoku.possible(i,j,nr): #line 34
self.sudo[i][j] = nr
Sudoku.solve()
self.sudo[i][j] = 0
return
if Sudoku.solution_number > 1: #if there is more than one solution, include solution number
print("Solution Number {}".format(Sudoku.solution_number))
else: print("Solution Number 1")
print(self.sudo)
Sudoku.add_sol_num()
@classmethod
def add_sol_num(cls):
cls.solution_number += 1
运行后:
s = Sudoku(su) #where su is a numpy array sudoku
s.solve() #line 52
我得到了错误:
File "/Users/georgesvanheerden/Python/Projects/Sudoku/SudokuSolver.py", line 52, in <module>
s.solve()
File "/Users/georgesvanheerden/Python/Projects/Sudoku/SudokuSolver.py", line 34, in solve
if Sudoku.possible(i,j,nr):
TypeError: possible() missing 1 required positional argument: 'num'
[Finished in 1.9s with exit code 1]
对不起,如果代码太多,我不知道该切掉哪些部分。
答案 0 :(得分:1)
在使用方法时使用self.possible
,Sudoku.possible会为您提供对该方法的引用,该引用找不到您正在从中调用它的实例。
这也适用于if Sudoku.solution_number > 1
,通常pythonic方法是使用self
变量或方法的第一个参数(尽管您也可以将self
传递给函数: Solution.possible(self, i, j , nr)
)
所以您的代码如下:
def solve(self):
for i in range(9):
for j in range(9):
if self.sudo[i][j] == 0:
for nr in range(1,10):
if self.possible(i,j,nr): #line 34
self.sudo[i][j] = nr
self.solve()
self.sudo[i][j] = 0
return
if self.solution_number > 1: #if there is more than one solution, include solution number
print("Solution Number {}".format(self.solution_number))
else: print("Solution Number 1")
print(self.sudo)
Sudoku.add_sol_num() # add_sol_num is a @classmethod
答案 1 :(得分:0)
您可以将self作为第一个参数:
restrain _ compose (generate _).tupled
答案 2 :(得分:0)
让我们首先了解所给出的错误:
TypeError: possible() missing 1 required positional argument: 'num'
这表示调用num
方法时possible()
参数没有值。
但是您要在此处传递3个参数:
if Sudoku.possible(i,j,nr)
那这里出了什么问题???
如果您看到方法的定义:
def possible (self, y, x, num):
这表示您将传递4个参数,其中一个将是类的实例/对象(self
参数)。
1. If we invoke this method using a class object, `self` argument is passed by default. So in this case we can just send 3 arguments apart from `self`.
2. If you want to invoke this method like you have done above, you will have to provide a value for `self` argument explicitally.
因此,这是您可以执行的操作(Python方式和好的方法):
在调用该方法的同时,请使用self
关键字。
if self.possible(i,j,nr):
答案 3 :(得分:0)
在第34和36行中,您将两个方法视为静态方法,因为它们是在类上而不是在某些实例上调用的。这也是为什么self
无法识别并因此在方法调用中要求另一个参数的原因。
您要调用Sudoku当前实例的方法。因此
if self.possible(i,j,nr):
第34行和
self.solve()
第36行应该可以解决问题。