Python错误:列表索引超出范围

Python错误:列表索引超出范围,python,python-2.7,Python,Python 2.7,我用python编写了一个战舰游戏。代码如下: #!/usr/bin/python """Battleship: Command Line Python Game This is a classical version of the battleship game. There is a board in which a battleship is randomly placed in a spot. User who correctly guesses the spot wins the

我用python编写了一个战舰游戏。代码如下:

#!/usr/bin/python

"""Battleship: Command Line Python Game

This is a classical version of the battleship game.

There is a board in which a battleship is randomly placed in a spot. User who correctly guesses the spot wins the game.

User have eight turns to guess correct cordinates

The game is coded in python 2"""

#imports randint from random to generate random numbers
from random import randint

#creating the board
board = []

#Filling up the board
for x in range(5):
  board.append(["O"] * 6)

#Function to print out the board to user
def print_board(board):
  for row in board:
    print " ".join(row)

#Call to print board to user
print_board(board)

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

#Function to generate random battleship coloumn
def random_col(board):
  return randint(0, len(board[0]) - 1)

#Call to generate random row and coloumn
ship_row = random_row(board)
ship_col = random_col(board)

#Snippet for debugging
print ship_row
print ship_col
#Snippet for debugging

#Running Turns
for turn in range(8):

  #Print out turn number to user
  print "Turn: " + str(turn + 1)

  #Accepts users guess
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  #Check if correct guess
  if guess_row == ship_row and guess_col == ship_col:
    #Congratulate the guess
    print "Congratulations! You sunk my battleship!"
    #Exit loop and Game Over
    print "Game Over"
    break

  #If not correct guess
  else:

    #Check weather input is valid
    if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
      #Inform an invalid input
      print "Oops, that's not even in the ocean."

    #Check weather repeated guess
    elif(board[guess_row][guess_col] == 'X'):
      #Inform a repeated guess
      print "You guessed that one already."

    #For valid new input
    else:
      #Inform a missed shot
      print "You missed my battleship!"
      #Fill guessed place with X and print out the board
      board[guess_row][guess_col] = 'X'
      print_board(board)

    #Check Weather out of Turns
    if turn == 7:
      #GAME OVER
      print "Game Over"
#/usr/bin/python
“战舰:命令行Python游戏”
这是战舰游戏的经典版本。
有一个棋盘,其中一艘战舰被随机放置在一个位置。猜对位置的用户将赢得游戏。
用户有八次机会猜出正确的坐标
游戏是用python 2编写的
#从random导入randint以生成随机数
从随机导入randint
#创建董事会
董事会=[]
#填满黑板
对于范围(5)内的x:
董事会追加([“O”]*6)
#向用户打印电路板的功能
def打印板(板):
对于板中的行:
打印“”连接(行)
#向用户调用打印板
印刷电路板
#生成随机战列舰行的函数
def随机_行(板):
返回randint(0,len(板)-1)
#生成随机战列舰列的函数
def随机_柱(板):
返回randint(0,len(板[0])-1)
#调用以生成随机行和列
船行=随机船行(板)
船列=随机列(船列)
#调试代码段
打印发货行
打印船型
#调试代码段
#连续转弯
对于交车范围(8):
#将轮次编号打印给用户
打印“圈数:”+str(圈数+1)
#接受用户猜测
猜测行=int(原始输入(“猜测行:”)
guess\u col=int(原始输入(“guess col:”)
#检查猜测是否正确
如果guess\u row==ship\u row和guess\u col==ship\u col:
#祝贺你猜对了
打印“祝贺你!你击沉了我的战舰!”
#退出循环,游戏结束
打印“游戏结束”
打破
#如果没有正确的猜测
其他:
#检查天气输入是否有效
如果(猜测行<0或猜测行>5)或(猜测列<0或猜测列>5):
#通知无效的输入
打印“哦,那甚至不在海里。”
#查天气反复猜测
elif(board[guess\u row][guess\u col]=“X”):
#反复猜测
打印“你已经猜到了。”
#获取有效的新输入
其他:
#报失
打印“你错过了我的战舰!”
#在猜测的地方填上X,然后打印出黑板
板[guess\u row][guess\u col]=“X”
印刷电路板
#轮流检查天气
如果匝数=7:
#游戏结束
打印“游戏结束”
它似乎显示出错误: 以下是终端错误的完整描述:

回溯(最近一次呼叫最后一次): 文件“/home/prem59/Dropbox/Python/Codecademy/battleship.py”,第77行,在 如果(电路板[猜测行][猜测列]=='X'): 索引器:列表索引超出范围


我认为elif的声明是在制造麻烦。但是如果我删除它,我将无法检查用户是否输入了重复输入。如何在不影响程序功能的情况下解决此问题?

您的电路板当前的大小为
5x6

这意味着行/列索引为:
0、1、2、3、4

检查猜测是否不正确的当前条件是:

if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
(不需要括号)


请注意,这完全是个人偏好,但我认为下面更可读:

if not (0 <= guess_row < 5 and 0 <= guess_col < 5):
如果不是(0
请注意,这个
for
循环为您提供了一个5x6(5行x 6列)电路板

if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
    ...
elif(board[guess_row][guess_col] == 'X'):
    ...
输入
(5,1)(5,2)(5,3)…
是您的板上没有的X行

快速修复:


并在相关的时候参考它们。这样,将来更容易改变你的棋盘大小或猜测的次数。

看看你创建棋盘的代码。它的尺寸是什么?(提示:什么是
range(5)
返回?
range(5)
会给你
[0,1,2,3,4]
。因此,您应该将其更改为
范围(6)
或更改有效性检查。谢谢。现在我解决了这个问题
for x in range(5):
  board.append(["O"] * 6)
if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5):
    ...
elif(board[guess_row][guess_col] == 'X'):
    ...
O O O O O O
O O O O O O
O O O O O O
O O O O O O
O O O O O O
X X X X X X
# Just add another row, the X row, and you're allset
for x in range(6):
      board.append(["O"] * 6)

# Check if the input == 5, print "not in the ocean", because it really is not.
if (guess_row < 0 or guess_row >= 5)
SIZE = 6
GUESSES = 8