"Game Over" if statement spacing


#1

The code works fine here:

    else:
      if turn == 3:
        print "Game Over"
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
    print_board(board)

BUT, if I move the if turn == 3 to the bottom, as shown below, it gives an unexpected indent error. What gives?

        else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
    print_board(board)
      if turn == 3:
        print "Game Over"

In both cases, the “if” is nested within else, so I thought it should be fine. What’s happening here? I just want “Game Over” to be displayed after the board is displayed.

Edit: crap… my spacing was butchered when copying it over. Uhh… This is the full block.

  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."
      if turn == 3:
        print "Game Over"
    elif board[guess_row][guess_col] == "X":
      print( "You guessed that one already." )
      if turn == 3:
        print "Game Over"
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
    print_board(board)
      if turn == 3:
        print "Game Over"

Maybe I should take a break and come back to this…


#2

Wouldn’t you say that’s sticking out on the left for no good reason though?

You’d change indentation when entering or exiting some block (like a function body, or a loop body), but that’s neither of those


#3

The reason it’s sticking out to the left (in my head, please correct me if I am wrong) is that regardless of the error or miss or condition, I want to print the board out after every guess. Are you saying it should be further to the left?


#4

Well, actually, you might be exiting the else-body there, but then you go ahead and indent the next line again


#5

You’d have to do:

board[guess_row][guess_col] = "X"
print_board(board)
if turn == 3:

Or:

else:
    ...
    board[guess_row][guess_col] = "X"
print_board(board)
if turn == 3:

right? (I’m completely ignoring what it would mean, just how it’s at all possible to indent)

can’t have this:

    x = 5
y = 7
    z = 3

#6

Oh I see. Thanks for the support. So what I did was accidentally exit the block and that caused an error.


#7

No clue. So far I’ve only looked at “uh why is that sticking out like that” … Doesn’t necessarily mean it’s all fixed ._. maybe it is.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.