Python战舰游戏索引超出范围错误

时间:2019-05-11 19:03:28

标签: python

我大约一周前才开始编码。我正在尝试编写一个非常简单的战舰游戏。它做了我想要做的事,但是当我测试它时,我得到的索引超出范围错误大约是十分之一。有什么建议吗?

谢谢:)

from random import randint, choice

x_cor = [0,1,2,3,4,5]
y_cor = [0,1,2,3,4]

def create_board():
    row = [["O" for i in x_cor] for x in y_cor]
    return row 

board = create_board()

def create_ship(board):
    board[choice(x_cor)][choice(y_cor)] = "*"
    return board

world = create_ship(board)

for i in world:
    print(" ".join(i))

1 个答案:

答案 0 :(得分:1)

更改:

def create_ship(board):
    board[choice(x_cor)][choice(y_cor)] = "*"
    return board

收件人:

def create_ship(board):
    board[choice(y_cor)][choice(x_cor)] = "*"
    return board

或者,
改变

x_cor = [0,1,2,3,4,5] 
y_cor = [0,1,2,3,4]

y_cor = [0,1,2,3,4,5] 
x_cor = [0,1,2,3,4]