gpt4 book ai didi

Python验证函数生成IndexError : list assignment index out of range

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:52 25 4
gpt4 key购买 nike

我正在用 Python 构建战舰游戏。我有一个列表,我正在尝试用 Python 构建一个验证工具来捕获超出列表 10x10 范围的用户输入。

这是代码:

from random import randint

player = "User"
board = []
board_size = 10

ships = {"Aircraft Carrier":5,
"Battleship":4,
"Submarine":3,
"Destroyer":3,
"Patrol Boat":2}

def print_board(player, board): # to print joined board
print("Here is " + player + "'s board")
for row in board:
print(" ".join(row))

def switch_user(player): # to switch users
if player == "User":
player = "Computer"
elif player == "Computer":
player = "User"
else:
print("Error with user switching")

for x in range(0, board_size): # to create a board
board.append(["O"] * board_size)

print_board(player,board)

def random_row(board): # generate random row
return randint(0, len(board) - 1)

def random_col(board): # generate random column
return randint(0, len(board[0]) - 1)

def user_places_ships(board, ships): # user choses to place its ships by providing starting co-ordinate and direction.
for ship in ships:
valid = False
while(not valid):
user_input_coordinates = input("Please enter row & column number for your " + str(ship) + ", which is " + str(ships[ship]) + "-cells long (row, column).")
ship_row, ship_col = user_input_coordinates.split(",")
ship_row = int(ship_row)
ship_col = int(ship_col)
user_input_dir = input("Please enter direction for your " + str(ship) + ", which is " + str(ships[ship]) + "-cells long (h for horizontal or v for vertical).")
valid = validate_coordinates(board, ships[ship], ship_row, ship_col, user_input_dir)
if not valid:
print("The ship coordinates either outside of" , board_size, "X" , board_size, "range, overlap with or too close to another ship.")
place_ship(board, ships[ship], ship_row, ship_col, user_input_dir)
print("You have finished placing all your ships.")

def validate_coordinates(board, ship_len, row, col, dir): # validates if the co-ordinates entered by a player are within the board and don't overlap with other ships
if dir == "h" or dir == "H":
for x in range(ship_len):
if row-1 > board_size or col-1+x > board_size:
return False
elif row-1 < 0 or col-1+x < 0:
return False
elif board[row-1][col-1+x] == "S":
return False
elif dir == "v" or dir == "V":
for x in range(ship_len):
if row-1+x > board_size or col-1 > board_size:
return False
elif row-1+x < 0 or col-1 < 0:
return False
elif board[row-1+x][col-1] == "S":
return False
return True

def place_ship(board, ship_len, row, col, dir): # to actually place ships and mark them as "S"
if dir == "h" or dir == "H":
for x in range(ship_len):
board[row-1][col-1+x] = "S"
elif dir == "v" or dir == "V":
for x in range(ship_len):
board[row-1+x][col-1] = "S"
else:
print("Error with direction.")
print_board(player,board)

user_places_ships(board,ships)

如果用户输入“10,10”作为船舶坐标,“h”作为水平方向,则 Python 会生成以下错误消息:

Traceback (most recent call last):   File "C:/Users/Elchin's PC/Downloads/battleships game.py", line 85, in <module>
user_places_ships(board,ships) File "C:/Users/Elchin's PC/Downloads/battleships game.py", line 49, in user_places_ships
valid = validate_coordinates(board, ships[ship], ship_row, ship_col, user_input_dir) File "C:/Users/Elchin's PC/Downloads/battleships game.py", line 62, in validate_coordinates
elif board[row-1][col-1+x] == "S": IndexError: list index out of range

我知道错误在这一行:

elif board[row-1][col-1+x] == "S":
return False

但我不知道如何解决它。您能帮我找出解决方案吗?

最佳答案

如果列表的长度为 n,您可以访问从 0 到 n-1(包含两者)的索引。

您的if然而语句检查:

if row-1+x > board_size or col-1 > board_size:  # greater than n
return False
elif row-1+x < 0 or col-1 < 0: # less than 0
return False
elif board[row-1+x][col-1] == "S":
return False

因此,如果我们到达最后一个 elif部分,我们保证索引是 0 < i <= n 。但这些应该是0 < i < n .

所以你应该改变第一个 if声明至:

if row-1+x >= board_size or col-1 >= board_size:  # greater than or equal n
return False
elif row-1+x < 0 or col-1 < 0: # less than 0
return False
elif board[row-1+x][col-1] == "S":
return False

你可以通过这样写来使代码更加优雅:

if not (0 < row-1+x < board_size and 0 < col-1 < board_size) or \
board[row-1+x][col-1] == "S":
return False

关于Python验证函数生成IndexError : list assignment index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44834873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com