Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用';str';对象不支持项分配“;_Python - Fatal编程技术网

Python &引用';str';对象不支持项分配“;

Python &引用';str';对象不支持项分配“;,python,Python,我不明白这个代码出了什么问题。大约有一半的时间我运行它,它给我一个'str'对象不支持项目分配错误。字符串应不重叠 from random import randint, sample board = [] for x in range(0, 5): board.append([","] * 11) board.append(",,,,,@,,,,,") for x in range(0, 5): board.append([","] * 11) def print_

我不明白这个代码出了什么问题。大约有一半的时间我运行它,它给我一个'str'对象不支持项目分配错误。字符串应不重叠

from random import randint, sample

board = []


for x in range(0, 5):
    board.append([","] * 11)

board.append(",,,,,@,,,,,")

for x in range(0, 5):
    board.append([","] * 11)

def print_board(board):
    for row in board:
        print " ".join(row)

x = sample(range(1, 11), 6)
y = sample(range(1, 11), 6)




board[x[0]][y[0]] = "A"
board[x[1]][y[1]] = "B"
board[x[2]][y[2]] = "C"
board[x[3]][y[3]] = "D"
board[x[4]][y[4]] = "E"
board[x[5]][y[5]] = "F"

print_board(board)

感谢您的帮助。

您的
板中的大多数行都是列表,但中间的一行只是一个字符串。试试这个:

board.append(list(",,,,,@,,,,,"))

您的运行时输出将非常方便。你应该解释你想得到什么和你实际得到什么。