-1
当我在codewars中输入我的代码时,我得到这个错误。 “回溯:模块 中的damaged_or_sunk IndexError:列表索引超出范围”。但是,当我在spyder3中尝试我的代码时,它工作得很好。虽然没有关于这个错误在函数damaged_or_sunk中的位置的指示器。字符串索引必须是整数战舰codewars kata
def damaged_or_sunk (board, attacks):
a = sum(x.count(1) for x in board)
b = sum(x.count(2) for x in board)
c = sum(x.count(3) for x in board)
a1 = 0
b1 = 0
c1 = 0
points = 0
sunk = 0
damaged = 0
not_touched = 0
for each in attacks:
rand_row = each[0]
rand_col = each[1]
if board[rand_row][rand_col] == 1:
a1 += 1
elif board[rand_row][rand_col] == 2:
b1 += 1
elif board[rand_row][rand_col] == 3:
c1 += 1
else:
pass
if a1 == a:
points += 1
sunk += 1
elif a1 == 0:
points -= 1
not_touched += 1
else:
points += 0.5
damaged += 1
if b1 == b:
points += 1
sunk += 1
elif b1 == 0:
points -= 1
not_touched += 1
else:
points += 0.5
damaged += 1
if c1 == c:
points += 1
sunk += 1
elif c1 == 0:
points -= 1
not_touched += 1
else:
points += 0.5
damaged += 1
return '{\'sunk\': %s, \'damaged\': %s, \'not_touched\': %s, \'points\': %s}' %(sunk, damaged, not_touched, points)
哪条线是错误?请提供[mcve]。 – Julien
它不会打印回CodeWars上发生错误的地方。它只是说,在模块中。 spyder3没有错误发生 –
如何构建'攻击'?任何代码示例? –