Game over


#1

Battleship: 16. Game Over
https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/game-over?action=lesson_resume

Since the site update, I’ve been having indentation problems. For this instance I keep getting this error: IndentationError: unexpected indent
At first I got it for line 42. (I changed nothing) Now I’m getting it for line 44.

from random import randint

board = []

for x in range(0, 5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

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

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

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



for turn in range(4):
  print "Turn", turn+1
  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 my battleship!"   
  else:
    if guess_row not in range(5) or \
      guess_col not in range(5):
      print "Oops, that's not even in the ocean."
    elif board[guess_row][guess_col] == "X":
      print( "You guessed that one already." )
    else:
     	print "You missed my battleship!"
     	board[guess_row][guess_col] = "X"
    print_board(board)
	  	if turn==3:
      	print "Game Over"



#2

well, the indent for if turn == 3: looks a bit off:

	  	if turn==3:

see how much indent?


#3

Should it be even with “print “You missed my battleship!””?


#4

not, it should be even with print_board(board)


#5

Adjusted and I still get the error. I feel like I’ve tried moving that line back and forth with both spaces and tabs, and still the error.


#6

yea, i experienced problems as well. sometimes i just removed the line so you get:

print_board(board) if turn==3:

then i put if on a separate line and re-indent it, that seems to work quit effective.


#7

That did not work for me. The only way I was able to get it to run without an error is to take if turn==3: out of the for loop.


#8

Was able to move on by having that out of the for loop. If that is correct, the instruction are rather confusing.


#9

it should be in the loop, not sure what went wrong. As long as you understand it and passed it, its fine :slight_smile:


#10

Thanks for confirming that I wasn’t doing it wrong. It’s frustrating when you’re learning and therefore unsure if you are doing it right. Appreciate the help!


#11