Please help me with this exercise, i am confused


#1



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


this is the error message I get : File "python", line 32
else:
^
IndentationError: unindent does not match any outer indentation level


I was altering a few things to get it to work and before my error was a syntax error. now it is this. could someone please give me the correct code as I am quite stuck on this. thank you


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


#2

Hi @hammaadkhan ,

Check for excessive indentation of the code below this elif header ...

elif guess_row not in range(5) or guess_col not in range(5):

#3

thank you, I fixed the indentation and I am now getting the same error as I did earlier 'File "python", line 36
elif board[guess_row][guess_col] == "X":
^
SyntaxError: invalid syntax'

what am I doing wrong?


#4

@hammaadkhan ,

Is your final elif block in the correct place, or should it be placed prior to the final else block?


#5

I've tried putting it several different places and all of them show this same message. I can't figure out where the correct location is. Thank you for your help


#6

Hi @hammaadkhan ,

You should revisit Exercise 12: Bad Aim, where the instructions begin by asking you to nest an if block ...

Add a new if: statement that is nested under the else.

If you reorganize the block structure of your code based on all of the instructions in that exercise, it may help you with the exercises that follow.


#7

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