花了很长时间来解决这个有关填充井字游戏棋盘的相对简单的问题。
# List variable ticTacToe should eventually
# hold [ [ a, b, c ], [ d, e, f ], [ g, h, i ]]
# to represent the Tic Tac to board:
# a b c
# d e f
# g h i
ticTacToe = [ [], [], [] ]
firstRow = input()
secondRow = input()
thirdRow = input()
ticTacToe.append(firstRow)
ticTacToe.append(secondRow)
ticTacToe.append(thirdRow)
#Output handled for you
for i in range(3) :
for j in range(3) :
print( "%3s" % ticTacToe[i][j], end="")
print()
输出已提供给我,无法替换。
我在这里有两个问题。
如果不删除方括号并重新开始,就无法获得[]中的行。如果要打印ticTacToe,我会得到[[], [], [], 'a,b,c', 'd,e,f', 'g,h,i']
而不是[[a,b,c], [d,e,f], [g,h,i]]
不需要的引号不断出现。如果first row = a,b,c
,当我将其附加到ticTacToe中时,它将显示为['a,b,c']
而不是[a,b,c]
不确定我要去哪里出错,我们将不胜感激。谢谢。
答案 0 :(得分:2)
您应该阅读有关列表的信息:PyTut lists
board = [ [], [], [] ] # a list of 3 other lists
# addd somthing to board:
board.append("something") # now its a list of 3 lists and 1 string
print(board)
board = board + ["otherthing"] # now its a list of 3 lists and 2 strings
print(board)
# modify the list inside board on place 0:
zero_innerlist = board[0] # get the list at pos 0 of board
print(board)
zero_innerlist.append("cat") # put something into that inner list
print(board)
zero_innerlist.append("dog") # put more into that inner list
print(board)
print(zero_innerlist) # print "just" the inner list
one_innerlist = board[1] # modify the 2nd inner list at pos 1
one_innerlist.append("demo")
print(board)
输出:
[[], [], [], 'something', 'otherthing'] # board
[[], [], [], 'something', 'otherthing'] # board
[['cat'], [], [], 'something', 'otherthing'] # board
[['cat', 'dog'], [], [], 'something', 'otherthing'] # board
['cat', 'dog'] # zero_innerlist
[['cat', 'dog'], ['demo'], [], 'something', 'otherthing'] # board
如果要将3个内容添加到每个内部列表中,则需要将3个追加添加到每个内部列表中。
其他不错的读物:string formatting和f-strings:
您正在使用2.7样式的打印,适用于3和3.6格式,并且f字符串更好:
board = [ ["a","b","c"], ["d","e","f"], ["g","h","i"] ]
for i in range(3) :
for j in range(3) :
print( f"{board[i][j]:3s}", end="")
print()
# or
for row in board:
for col in row:
print( f"{col:3s}", end="")
print()
# or
for row in board:
print( f"{row[0]:3s}{row[1]:3s}{row[2]:3s}")
# or
print( '\n'.join( ( ''.join(f"{col:3s}" for col in row ) for row in board) ))
输出(全部):
a b c
d e f
g h i
答案 1 :(得分:1)
您可以使用一个简单的循环遍历输入,并在ticTacToe
列表中追加:
ticTacToe = []
for x in input('Enter rows (each element separated by comma) separated by space: ').split():
ticTacToe.append(x.split(','))
print(ticTacToe)
#Output handled for you
样品运行:
Enter rows (each element separated by comma) separated by space: a,b,c d,e,f g,h,i
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
ticTacToe = [x.split(',') for x in input('Enter rows (each element separated by comma) separated by space: ').split()]
答案 2 :(得分:0)
首先,在python中,引号表示一个字符串,因此您必须将其放在其中。
由于ticTacToe
是列表列表,因此您要将输入附加到最外面的列表。要附加到内部列表中:
ticTacToe = [ [], [], [] ]
firstRow = input()
secondRow = input()
thirdRow = input()
ticTacToe[0].append(firstRow)
ticTacToe[1].append(secondRow)
ticTacToe[2].append(thirdRow)
# ticTacToe >>> [['a,b,c'], ['d,e,f'], ['g,h,i']]
但是,从输出代码开始,看来这不是您的老师想要您做的。
相反,每个列表都需要包含一个字符,而不是整个字符串。
它看起来像这样:
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
有很多方法可以做到这一点,但这是其中一种:
ticTacToe = [[], [], []]
firstRow = input()
secondRow = input()
thirdRow = input()
ticTacToe[0] = firstRow.split(",")
ticTacToe[1] = secondRow.split(",")
ticTacToe[2] = thirdRow.split(",")
split
方法采用字符串并将其转换为提供定界符(在这种情况下为',
')的列表。然后将其分配(而不是追加)到内部列表。 (注意:如果在逗号后加一个空格,这是行不通的,但是我会让您知道)。