def random_row(board, length):
return randint(0, len(board) - length)
def random_col(board, length):
return randint(0, len(board[0]) - length)
ship_all = []
for length in range(1,6):
ship = []
ship_row = random_row(board, length)
ship.append(ship_row)
ship_col = random_col(board, length)
ship.append(ship_col)
i = 0
if randint(0,1) == 0:
while i<length:
ship_all.append(ship)
ship[0] += 1
print ship
i+=1
else:
while i<length:
ship_all.append(ship)
ship[1] += 1
print ship
i+=1
print ship_all
ship_all.append(ship)只是给出了船的最终结果,但打印船正常运行,如何解决这个问题?
答案 0 :(得分:2)
您没有将ship
的副本添加到ship_all
,而是一遍又一遍地添加相同的列表对象,每次迭代都会更改该对象。
每次追加时创建一个copy of the list:
ship_all.append(ship[:])
或在ship
循环内从头开始创建while
列表。