列表在每次更新 Python 后重置,任何不重置但仍然更新的方式
List is resetting after every update Python, any way to make not reset and still update
我正在尝试制作一个数独解算器,它可以在数独应用程序中为棋盘拍照,然后进行解算。我已经制作了解决它的实际程序并且它有效。我现在只是想将应用程序中的电路板放入代码中,我正在通过 pyautogui 这样做,并使用效果很好的数字图像中心位置。 Yt 只是在我对板进行每次更新后,板一直在重置。
棋盘的起始位置为:
board = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
])
但是当我 运行 代码结束时是这样的:
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
- - - - - - - - - - - - -
0 0 9 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 9 0
- - - - - - - - - - - - -
0 0 0 | 0 0 9 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
它只保留最后一个 运行 数字 (9) 在板上。有什么方法可以防止这种情况发生并在每次更新后保存板上的所有数字吗?
使看板更新的代码:
def place_number(num, pos1, pos2):
global board
if num == one:
board[pos2, pos1] = 1
if num == two:
board[pos2, pos1] = 2
if num == three:
board[pos2, pos1] = 3
if num == four:
board[pos2, pos1] = 4
if num == five:
board[pos2, pos1] = 5
if num == six:
board[pos2, pos1] = 6
if num == seven:
board[pos2, pos1] = 7
if num == eight:
board[pos2, pos1] = 8
if num == nine:
board[pos2, pos1] = 9
else:
board[pos2, pos1] = 0
已解决,只需要从 place_number 函数中删除 else 语句,该函数正在重置除我 运行 通过的数字之外的所有 board_locations。
def place_number(num, pos1, pos2):
global board
if num == one:
board[pos2, pos1] = 1
if num == two:
board[pos2, pos1] = 2
if num == three:
board[pos2, pos1] = 3
if num == four:
board[pos2, pos1] = 4
if num == five:
board[pos2, pos1] = 5
if num == six:
board[pos2, pos1] = 6
if num == seven:
board[pos2, pos1] = 7
if num == eight:
board[pos2, pos1] = 8
if num == nine:
board[pos2, pos1] = 9
我正在尝试制作一个数独解算器,它可以在数独应用程序中为棋盘拍照,然后进行解算。我已经制作了解决它的实际程序并且它有效。我现在只是想将应用程序中的电路板放入代码中,我正在通过 pyautogui 这样做,并使用效果很好的数字图像中心位置。 Yt 只是在我对板进行每次更新后,板一直在重置。 棋盘的起始位置为:
board = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
])
但是当我 运行 代码结束时是这样的:
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
- - - - - - - - - - - - -
0 0 9 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 9 0
- - - - - - - - - - - - -
0 0 0 | 0 0 9 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
0 0 0 | 0 0 0 | 0 0 0
它只保留最后一个 运行 数字 (9) 在板上。有什么方法可以防止这种情况发生并在每次更新后保存板上的所有数字吗?
使看板更新的代码:
def place_number(num, pos1, pos2):
global board
if num == one:
board[pos2, pos1] = 1
if num == two:
board[pos2, pos1] = 2
if num == three:
board[pos2, pos1] = 3
if num == four:
board[pos2, pos1] = 4
if num == five:
board[pos2, pos1] = 5
if num == six:
board[pos2, pos1] = 6
if num == seven:
board[pos2, pos1] = 7
if num == eight:
board[pos2, pos1] = 8
if num == nine:
board[pos2, pos1] = 9
else:
board[pos2, pos1] = 0
已解决,只需要从 place_number 函数中删除 else 语句,该函数正在重置除我 运行 通过的数字之外的所有 board_locations。
def place_number(num, pos1, pos2):
global board
if num == one:
board[pos2, pos1] = 1
if num == two:
board[pos2, pos1] = 2
if num == three:
board[pos2, pos1] = 3
if num == four:
board[pos2, pos1] = 4
if num == five:
board[pos2, pos1] = 5
if num == six:
board[pos2, pos1] = 6
if num == seven:
board[pos2, pos1] = 7
if num == eight:
board[pos2, pos1] = 8
if num == nine:
board[pos2, pos1] = 9