Python 2 course Battleship exercise - problem with the extra exercises

Hello everyone,

I’ve tried to do the extra exercises that are coming with the Battleship! lesson of the python 2 course.
After succesfully creating a 2 player friendly environment I’m struggling with the code that allows 2 battleships to be used on the board.

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank 1 battleship!"
    board[guess_row][guess_col] = "H"
    if board[ship_row][ship_row_2][ship_col][ship_col_2] == "H":
      print "You sank both battleships - You win"
      print_board(board)
    else:
      print_board(board)
        
  elif guess_row == ship_row_2 and guess_col == ship_col_2:
    print "Congratulations! You sank 1 battleship!"
    board[guess_row][guess_col] = "H"
    if board[ship_row][ship_row_2][ship_col][ship_col_2] == "H":
      print "You sank both battleships - You win"
      print_board(board)
    else:
      print_board(board)
  

First i put the part where it verifies if 2 ships have been (which is just a boolean that checks if both of the ship locations in the list which happens to be the board are marked with an X) hit over the rest of the code but then and now i got this error:

Traceback (most recent call last): File “python”, line 52, in <module> IndexError: string index out of range

Here is the full code if it can help:


print """Where are the battleships?
Try your luck
"""
print_board(board)

def random_row(board):
  return randint(0, len(board) - 3)

def random_row_2(board):
  return randint(3, len(board) - 1)
  
def random_col(board):
  return randint(0, len(board[0]) - 3)

def random_col_2(board):
  return randint(3, len(board[0]) - 1)

ship_row = random_row(board)
ship_row_2 = random_row_2(board)
ship_col = random_col(board)
ship_col_2 = random_col_2(board)


print ship_row
print ship_col

print ship_row_2
print ship_col_2

# Everything from here on should be in your for loop
# don't forget to properly indent!
for turn in range(3):
  print "Turn", turn + 1
  print "Player 1 - Your turn"
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))
  
  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank 1 battleship!"
    board[guess_row][guess_col] = "H"
    if board[ship_row][ship_row_2][ship_col][ship_col_2] == "H":
      print "You sank both battleships - You win"
      print_board(board)
    else:
      print_board(board)
        
  elif guess_row == ship_row_2 and guess_col == ship_col_2:
    print "Congratulations! You sank 1 battleship!"
    board[guess_row][guess_col] = "H"
    if board[ship_row][ship_row_2][ship_col][ship_col_2] == "H":
      print "You sank both battleships - You win"
      print_board(board)
    else:
      print_board(board)
  
   
        
  else:
        
    if guess_row not in range(5) or \
      guess_col not in range(5):
      print "Oops, that's not even in the ocean."
      print_board(board)
    elif board[guess_row][guess_col] == "X":
      print( "You guessed that one already." )
      print_board(board)
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
      print_board(board)
      if (turn == 2):
        print "Game Over - Player 2 it's your turn"
        print_board(board)
          
        for turn in range(3):
        
          print "Turn", turn + 1
          print "Player 2 - Your turn"
          guess_row = int(raw_input("Guess Row: "))
          guess_col = int(raw_input("Guess Col: "))
            
          if board[ship_row][ship_row_2][ship_col][ship_col_2] == H:
            print "You sank both battleships - You Win"
            print_board(board)
            break
     
            
          elif guess_row == ship_row and guess_col == ship_col:
            print "Congratulations! You sank 1 battleship!"
            board[guess_row][guess_col] = "H"
            print_board(board)
                
          elif guess_row == ship_row_2 and guess_col == ship_col_2:
            print "Congratulations! You sank 1 battleship!"
            board[guess_row][guess_col] = "H"
            print_board(board)
          else:
            if guess_row not in range(5) or \
              guess_col not in range(5):
              print "Oops, that's not even in the ocean."
              print_board(board)
            elif board[guess_row][guess_col] == "X":
              print( "Player 1 guessed that already!." )
              print_board(board)
            elif board[guess_row][guess_col] == "x":
              print "You guessed that one already!"
              print_board(board)
            else:
              print "You missed my battleship!"
              board[guess_row][guess_col] = "x"
              print_board(board)
              if (turn == 2):
                print "Game Over"

I appreciate your help

2 Likes

if board[ship_row][ship_row_2][ship_col][ship_col_2] == “H”:

are you trying to test if both ships are hit? maybe try

if board[ship_row][ship_col] == “H” and board[ship_row_2][ship_col_2] == “H”:

2 Likes

That solved the problem, thanks.

1 Like