【发布时间】:2016-10-27 04:55:11
【问题描述】:
我在制作一个简单的战舰游戏时遇到了这个问题。这是我的代码:
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)
我试图重新分配导弹击中“X”的位置的“O”,但随后它显示
TypeError: 'str' 对象不支持项目分配。
【问题讨论】: