在Python初学者CS作业题中填充字符串数组

2024-06-01 01:20:09 发布

您现在位置:Python中文网/ 问答频道 /正文

花了很长一段时间与这个相对简单的问题有关填充井字游戏趾板挣扎。你知道吗

# 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()

输出已给我,无法替换。你知道吗

我有两个问题。你知道吗

  1. 如果不删除括号并重新开始,就无法获取[]中的行。如果我要打印ticTacToe,我会得到[[], [], [], 'a,b,c', 'd,e,f', 'g,h,i'],而不是[[a,b,c], [d,e,f], [g,h,i]]

  2. 不需要的引号不断出现。如果first row = a,b,c,当我将它附加到ticTacToe中时,它显示为['a,b,c'],而不是[a,b,c]

我不知道我哪里出错了,任何帮助都将不胜感激。谢谢。你知道吗


Tags: toin游戏forinputrangelisttictactoe
3条回答

您应该阅读列表: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 formattingf-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  

首先,在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方法接受一个字符串并将其转换为一个列表,该列表提供了一个分隔符,在本例中是一个,。然后它将分配(而不是附加)到内部列表。(注意:如果你在逗号后面加空格,这是行不通的,但我会让你算出那个)

您可以使用一个简单的循环,通过按空格拆分的输入并附加到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()]

相关问题 更多 >