Not Again


#1



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



[https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/4?


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


#2

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 !!


#3

@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:
else:

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:
   else:
elif something_1:
   if something:
else:
  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:
else:
elif something else:

That's really wrong!

Hope that helps you!


#4

i didn't talk about print statement :wink:


#5

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


#6

good luck :smiley: .


#7

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