シンプルな戦艦ゲームを作成する際にこの問題が発生します。ここに私のコードです:Python:TypeError: 'str'オブジェクトがアイテム割り当てをサポートしていません
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)
私は再割り当てしようとした「Oのところでヒットミサイル 'X' が、その後、それは言う
TypeError: 'str' object does not support item assignment.
ありがとうございます。私はそれらを結合することが文字列にそれらを作ることを知らなかった。なぜ私はOをXと置き換えることができないのか、今理解しています。 – Inas