我正在尝试打印一个简单的游戏板。
places = [" "] * 9
board = (places[8] + "|" + places[7] + "|" + places[6] + "\n",
places[5] + "|" + places[4] + "|" + places[3] + "\n",
places[2] + "|" + places[1] + "|" + places[0] + "\n")
print(board)
但是我得到的不是董事会:
(' | | \n', ' | | \n', ' | | \n')
我想不带括号而是用新行而不是\ n打印它。我该怎么办?
答案 0 :(得分:1)
您只需要使用以下行进行打印:
print(*board,sep="")
说明:运算符*是解包运算符:元组board
然后被解包,就像您已经完成了:
print( places[8] + "|" + places[7] + "|" + places[6] + "\n",
places[5] + "|" + places[4] + "|" + places[3] + "\n",
places[2] + "|" + places[1] + "|" + places[0] + "\n",
sep="")
答案 1 :(得分:1)
不打印整个元组,但是它是元素,例如:
for i in board:
print(i)
答案 2 :(得分:0)
因为您已在方括号中的每行末尾添加了逗号,所以您制作了一个元组,该元组是可迭代的,就像列表一样。只需在下一行的开头用+代替逗号
state='{"instanceId":"iaFMRu32kedGmOHC8umMQ","**dataField**":"{"id":52,"name":"CropSelection"}","validation":"incomplete","required":true,"value":"","hasValue":false,"requiresSave":true}';
这将创建一个字符串而不是一个元组。
答案 3 :(得分:0)
places = [" "] * 9
print(places[8] + "|" + places[7] + "|" + places[6] + "\n" + places[5] + "|" +
places[4] + "|" + places[3] + "\n" + places[2] + "|" + places[1] + "|" + places[0] +
"\n")
这就是您要寻找的。 p>
问题在于“ \ n”仅在 打印语句中起作用。