Not Again


Maybe I've been looking at this too long. I'm stuck like Chuck, can someone help me on this? I'm getting a syntax error on line 39


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)


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)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row:
    print "Congratulations! You sank my battleship"
    print "You missed my battleship!"
    board[guess_row] [guess_col] == "X"
    if guess_row not in range(5) or \
       guess_col not in range(5):
        print "Oops, that's not even in the ocean."
        print "You missed my battleship!"
    elif board[guess_row][guess_col] == 'X':
        print "You guessed that one already."
        print "You missed my battleship!"
    print print_board(board)


i didn't run your code ,but i think you did repeat this

so remove it
also this , because you should set X to guess row , col if he miss !!


@azazaz: Repeating a print statement is not actually a problem that will affect how your code will work, probably will get the user to be confused .

@jermiside: The way we work with conditionals is like this:

if statement:
elif statement:

You can't have a else before an elif, considering they're in the exact same scope.
But you can have a entire instruction, finishing with an else, but as long as it's inside another scope.

Got it? No?
Here's an example:

if something:
   if something else:
elif something_1:
   if something:
  print ("Welp, this was interesting")

See? Each statement is in a different scope and such is okay, you can do this.
But the other way around such as:

if something:
elif something else:

That's really wrong!

Hope that helps you!


i didn't talk about print statement :wink:


@azazaz: Yeah but I was talking about that and explaining the conditional order about if/elif/else :wink:


good luck :smiley: .


@g4be @azazaz Thank you guys for the help and explanation! Code works after I removed the unnecessary else statement.