Elif error


#1

Hello,
I am doing the exercise
https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/4?curriculum_id=4f89dab3d788890003000096#
and my code is

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

but I am getting error on my last elif ststement. i am not sure why

File "python", line 39
elif board[guess_row][guess_col] == "X" :
^
SyntaxError: invalid syntax


#2

your code is a bit of a mess, look at this pseudo so you can properly structure your code:

# when the user guess correctly
if hit ship:
     print "let the user know they won"
else:
    # this else clause handles all the different scenarios for a miss
    # first verify what the shot is in the board
    if not in range:
        print "that is not even in the ocean"
    # now lets verify the user hasn't guessed this coordinates before
    elif already guessed:
        print "you entered this coordinates before"
    # finally, the use just missed with a valid shot and not guessed it before
     else:
        print "inform the user they missed"
        # updated the board
        # print the updated board

hope this will help your understanding of your program
`


#3

Thanks I corrected and the correct code is

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

#4

my example even gave correct indention for all the scenarios?

lets walk through it:

# Write your code below!
if guess_row==ship_row and guess_col==ship_col :
    print "Congratulations! You sank my battleship!"

okay, the user guessed the ship, so far so good, next:

else :
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

so this else will handle a miss. But before we inform the user he missed, before we update the board and print the board, we should first verify the shot is on the board:

else :
    print "You missed my battleship!"   # remove this line, its to early
    board[guess_row][guess_col] = "X"   # remove this line, its to early
    print_board(board)    # remove this line, to early as well
if guess_row not in range(5) or guess_col not in range(5) :
        # if is wrongly indented, it should be inside else.
        print "Oops, that's not even in the ocean."

see the comments. Especially updating the board (which is this line:

board[guess_row][guess_col] = "X"

) is risky, given python will given an index error, your program will crash

so first we need to verify shot is on the board/in ocean (indention of this line is off, it should be in the else clause which handles incorrect guesses), then verify it hasn't been guessed before. Look at my pseudo code again, also the indention


#5

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