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

拉姆(Prem Raj)

我用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"

似乎显示错误:这是终端错误的完整描述:

追溯(最近一次通话最近):文件“ /home/prem59/Dropbox/Python/Codecademy/battleship.py”,第77行,位于if(board [guess_row] [guess_col] =='X'):IndexError:列表索引超出范围

我认为elif语句正在引起麻烦。但是,如果删除它,将无法检查用户是否输入了重复输入。如何解决此问题而不影响程序的功能?

噻吩
for x in range(5):
  board.append(["O"] * 6)

请注意,此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)...这将被评估到elif块,因为5 > 5False

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

输入的(5, 1) (5, 2) (5, 3)...是您的电路板没有的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

并在相关时参考它们。这样一来,将来更改董事会规模或猜测数目就变得更加容易。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章