0

I have made a 'battleship' program via Python and decided to structure my code (as previously it was messy but worked fine). However whenever I run my code it now pops up with an indentation error:

"File "python", line 29
guess_col = int(raw_input("Guess Col:"))
IndentationError: unindent does not match any outer indentation level"

I searched around and realized I had used some spaces as well as tabs so I switched it purely to tabs (indent by 4 spaces). However the problem still persisted and I don't understand why. Is anyone able to share why this could be?

from random import randint
board = []
turn = 0
def print_board(board):
   for row in board:
       print " ".join(row)
def random_row(board):
    return randint(0, len(board) - 1)
def random_col(board):
    return randint(0, len(board) - 1)

#Start Battleship Functionality Here
print "Let's play Battleship!"
for x in range(5):
    board.append(["O"] * 5)
    print_board(board)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col


for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    print "Oops, that's not even in the ocean."
    print "Turn", turn + 1
elif (board[guess_row][guess_col] == "X"):
    print "You guessed that one already."
    print "Turn", turn + 1
elif guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    print_board(board)
    break
elif turn == 3:
    print "Game Over"
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print "Turn", turn + 1
    print_board(board)
6
  • 1
    Aside: The common standard for Python is to use spaces exclusively. See python.org/dev/peps/pep-0008/#tabs-or-spaces Also, the code you've pasted above seems to be a mix of spaces and tabs.
    – Robᵩ
    Oct 5, 2015 at 2:51
  • Oh thanks for that Rob! Would you have any idea why this error occurs?
    – IronKirby
    Oct 5, 2015 at 2:54
  • Probably because you still have a mix of tabs and spaces.
    – Robᵩ
    Oct 5, 2015 at 2:54
  • I ran your code and re-indented the problem line and it ran fine Oct 5, 2015 at 2:55
  • 1
    Also, watch out when you are re-indenting stuff because I think all those if statements need to be forward one level Oct 5, 2015 at 2:56

1 Answer 1

0

The problem was in the For Statement - because my IF was not indented properly underneath the for loop the indentation levels did not match. Correcting it to this resulted in the program running smoothly.

    for turn in range(4):
        guess_row = int(raw_input("Guess Row:"))
        guess_col = int(raw_input("Guess Col:"))

        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
            print "Turn", turn + 1
            etc....

Thanks for everyones input!

Not the answer you're looking for? Browse other questions tagged or ask your own question.