Not again! (invalid syntax)


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/4?curriculum_id=4f89dab3d788890003000096#


File "python", line 41
elif(board[guess_row][guess_col] == "X"):
^
SyntaxError: invalid syntax
It is actually ending underneath the guess_col underscore

Would expect it to print.


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

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"

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


#2

The sequence of if/elif../else goes like this..

if -- > elif --> elif ---- any numbers of elif ---> else

You're introducing elif after else so Its throwing the error.


#3

Just updated it both ways

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

kicks back
File "python", line 38
elif:
^
SyntaxError: invalid syntax
two elses kicks back

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

File "python", line 41
else(board[guess_row][guess_col] == "X"):
^
SyntaxError: invalid syntax


#4

One important thing to note that our if and elifs will have conditional statements attached to them..on the other hand else goes without any conditional statement.

if (condition)-- > elif (condition) --> elif (condition) ---- any numbers of elif (condition) ---> else


#5

I get it now. Fixed.


#6

What's wrong?


#7

hi @raghavjain97,
Python have elif not else if like many other languages .
else if is written elif so the spaces does not go out of structure (Irregular Indentation).


#8

hi @greentreemee

I tried using that to. Didn't work :frowning:


#9

@greentreemee

■■■■! Now it did. Thanks!


#10

:thumbsup: ,It happens when we move from language like C(++) or Java to Python, we used to naturally make that mistake. :slight_smile:


#11

Ah! Thanks, I will keep that in mind!


#12

Lol happened a lot with me though ,sometimes I mixed them then halfway through realized I was writing Python XD :grinning:


#13

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