【发布时间】:2022-01-27 13:35:10
【问题描述】:
我正在为学校创建数独求解器,但无法将最终产品输出到文本文件中。我现在已经尝试了几件事,但每次它要么有错误,要么只是在 python 上解决它。如何让它进入文本文件?感谢您的帮助!
def print_board(pr):
with open('writefile','w') as file:
for r in range(len(pr)):
if r % 3 == 0 and r != 0:
print("- - - - - - - - - - - - - ")
for c in range(len(pr[0])):
if c % 3 == 0 and c != 0:
print(" | ", end="")
if c == 8:
print(pr[r][c])
else:
print(str(pr[r][c]) + " ", end="")
def find_empty(pr):
for r in range(len(pr)):
for c in range(len(pr[0])):
if pr[r][c] == 0:
return (r, c) # row, col
return None
solve(board)
print_board(board)
【问题讨论】:
-
这能回答你的问题吗? Python how to write to a binary file?