Not again! 13 level


#1

Please help! I'm stack

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

and I get the error massage:
File "python", line 38
** elif:**
** ^**
SyntaxError: invalid syntax


#2

you have an elif without a condition after an else which the exercise correctly states is an error in the syntax. The syntax is that you start with an if with a condition and then you deal with the rest:
- if there is nothing else to do: fine, just use if with condition
- if for any other case something should be done: else no condition
- if the rest should be divided further with condition: elif with condition

So it always goes:

if condition:
    do stuff
elif condition2: # [optional] and maybe several times
    do other stuff
else: # [optional] at the very end and without condition 
    for anything else do this

#3

ok thanks, but i change it and I got the same error..

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

#4

Well you just fixed the order of statements but your elif still lacks a condition.


#5

please help meeeeeeeeeee hahaha
can you please write your code?


#6

look at what you've got in your if statement.
The structure of the IF ... ELSE or the IF ... ELSE IF ... ELSE is the same from excel / vb to python to php and beyond.

Structure is:
IF something is true:
do something
ELSE:
do something else

or

IF something is true:
do something
ELSE IF some other thing is true:
do something else
ELSE:
do something else

Back to what you've written.
You have the test for true missing in your ELSE IF

Note in Python you can use ELIF instead of ELSE IF


#7

Here is my code:

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

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 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!"
board[guess_row][guess_col] = "X"
print_board(board)


#8

thanks a lot for you guys!!!


#9

here

        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) and (guess_col == ship_col):
            print "Congratulations! You sank my battleship!"
        else:
            if(guess_row!=range(5) or guess_col!=range(5)):
                print "Oops, that's not even in the ocean."
            elif(board[guess_row][guess_column] == "X"):
                print "You guessed that one already."
            else:
                print "You missed my battleship!"
                board[guess_row][guess_col] = "X"
                print_board(board)

#10

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