我一直在写这段代码大约一个小时,我无法完成最后一步。我需要修改下面的代码。从我尝试和阅读的内容来看,它位于前3行中的某个位置(但整个代码可能需要修改)。我需要将每个输入行转换为列表,将其附加到电路板列表,并在最后返回一个数独板。
最后的输出应为Enter the file for the initial S board ==>
;然后我输入board3.txt
(包括在帖子的末尾)并获得我在此代码中制作的图表。
def read_board(fn):
board = []
for line in open(fn,'r'):
# FIXME
def print_board( board ):
for r in range(0,9):
if r%3 == 0:
print '-'*25
print '|',
for c in range(0,9):
print board[r][c],
if c==2 or c==5:
print '|',
elif c==8:
print '|'
print '-'*25
def ok_to_add(row,col,num,board):
return True
if __name__ == "__main__":
name = raw_input("Enter the file for the initial S board ==> ").strip()
board = read_board(name)
print_board(board)
board3.txt
:
1 . . . 2 . . 3 7
. 6 . . . 5 1 4 .
. 5 . . . . . 2 9
. . . 9 . . 4 . .
. . 4 1 . 3 7 . .
. . 1 . . 4 . . .
4 3 . . . . . 1 .
. 1 7 5 . . . 8 .
2 8 . . 4 . . . 6
答案 0 :(得分:1)
您只需要split
每行。它将自动按行划分行,并将其列入一个列表。
def read_board(fn):
with open(fn, 'r') as file:
return [line.split() for line in file]