Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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:TypeError:';str';对象不支持项分配_Python_Typeerror - Fatal编程技术网

Python:TypeError:';str';对象不支持项分配

Python:TypeError:';str';对象不支持项分配,python,typeerror,Python,Typeerror,我在制作一个简单的战舰游戏时遇到了这个问题。这是我的密码: board = [] row = ['O'] * 5 #<<<<determine the board size here joined_O = ' '.join(row) for i in range(5): #<<<<determine the board size here board.append(joined_O) print(joined_O) fro

我在制作一个简单的战舰游戏时遇到了这个问题。这是我的密码:

board = []
row = ['O'] * 5 #<<<<determine the board size here 
joined_O = '  '.join(row)


for i in range(5): #<<<<determine the board size here
    board.append(joined_O)
    print(joined_O)

from random import randint #<<<< this code is to determine where the ship is. It is placed randomly.
ship_row = randint(1,len(board))
ship_col = randint(1,len(board))

print(ship_row,', ',ship_col,'\n')

print('Shoot missile to the ship')
missile_row = int(input('row   : '))
missile_col = int(input('column: '))

#I really don't know where you're supposed to put the int() thingy so i put it everywhere
if int(missile_row) == int(ship_row) and int(missile_col) == int(ship_col):
    print("Congratulation! You've hit the ship.")
    break
elif int(missile_row) >= len(board) or int(missile_col) >= len(board):
    print('Sorry! Area is out of range.')
    break
else:
    print('Missile missed the target')
    board[int(missile_row)][int(missile_col)] = 'X'
    print(board)
board=[]
行=['O']*5#查看:

board = []
row = ['O'] * 5 #<<<<determine the board size here 
joined_O = '  '.join(row)
不是有效的命令,因为它试图修改board list中的字符串而不是2D list中的元素。在Python中,字符串是不可变的,因此不能在适当的位置更改它们的字符

简而言之,board不是代码中的2D列表,而是字符串列表。

请看:

board = []
row = ['O'] * 5 #<<<<determine the board size here 
joined_O = '  '.join(row)
for i in range(5): #<<<<determine the board size here
    board.append(joined_O)
不是有效的命令,因为它试图修改board list中的字符串而不是2D list中的元素。在Python中,字符串是不可变的,因此不能在适当的位置更改它们的字符

简而言之,board不是代码中的2D列表,而是字符串列表

for i in range(5): #<<<<determine the board size here
    board.append(joined_O)
这至少是正确的类型。但是你会有奇怪的虫子,当你错过一艘船时,会出现五个X而不是一个。这是因为每一行都是同一行;改变一个人,就改变了所有人。可以通过每次使用切片技巧复制行来避免这种情况

for i in range(5): #<<<<determine the board size here
    board.append(row[:])
现在您已经获得了一些非常好的输出

Shoot missile to the ship
row   : 1
column: 1
Missile missed the target
O  O  O  O  O
O  X  O  O  O
O  O  O  O  O
O  O  O  O  O
O  O  O  O  O
这至少是正确的类型。但是你会有奇怪的虫子,当你错过一艘船时,会出现五个X而不是一个。这是因为每一行都是同一行;改变一个人,就改变了所有人。可以通过每次使用切片技巧复制行来避免这种情况

for i in range(5): #<<<<determine the board size here
    board.append(row[:])
现在您已经获得了一些非常好的输出

Shoot missile to the ship
row   : 1
column: 1
Missile missed the target
O  O  O  O  O
O  X  O  O  O
O  O  O  O  O
O  O  O  O  O
O  O  O  O  O

兄弟,你刚刚修复了他代码中的所有错误,让他失去了学习python的机会。也许。。。但编程的好处在于,未来总会有更多的bug/学习机会:-)@Wajahat他在这里做了一些很好的教学活动-至少分解了代码,消除了复制粘贴整个程序的机会。嗨!谢谢你花时间回答这个问题。不过我有几个问题,因为有些问题我还是不懂。1.如果我不使用切片技巧,为什么如果我在线路板列表中添加五次,每一行仍然是同一行?这不意味着我在空列表中添加了5个不同的列表吗?我尝试过使用切片技巧,但它奏效了。我只是还不知道是什么让它与众不同。我之所以使用joined_o变量,是为了使电路板从一开始就没有引号,而不仅仅是最后一位。您是否将末尾的行连接起来,使它们不会成为字符串?我打算重复使用输入函数,所以如果电路板成为字符串列表,我就不可能在电路板中再放一个“X”了,对吗?如果你不明白我的意思,请告诉我,这样我可以详细说明。非常感谢你!兄弟,你刚刚修复了他代码中的所有错误,让他失去了学习python的机会。也许。。。但编程的好处在于,未来总会有更多的bug/学习机会:-)@Wajahat他在这里做了一些很好的教学活动-至少分解了代码,消除了复制粘贴整个程序的机会。嗨!谢谢你花时间回答这个问题。不过我有几个问题,因为有些问题我还是不懂。1.如果我不使用切片技巧,为什么如果我在线路板列表中添加五次,每一行仍然是同一行?这不意味着我在空列表中添加了5个不同的列表吗?我尝试过使用切片技巧,但它奏效了。我只是还不知道是什么让它与众不同。我之所以使用joined_o变量,是为了使电路板从一开始就没有引号,而不仅仅是最后一位。您是否将末尾的行连接起来,使它们不会成为字符串?我打算重复使用输入函数,所以如果电路板成为字符串列表,我就不可能在电路板中再放一个“X”了,对吗?如果你不明白我的意思,请告诉我,这样我可以详细说明。非常感谢你!非常感谢您的回答!我不知道加入他们会使他们成为一个字符串。我现在明白了为什么我不可能用X来代替O,所以谢谢你!非常感谢您的回答!我不知道加入他们会使他们成为一个字符串。我现在明白了为什么我不可能用X来代替O,所以谢谢你!