Syntax error on Battleship 15


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/play-it-sam

<In what way does your code behave incorrectly? Include ALL error messages.>

Currently getting syntax error:

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

<What do you expect to happen instead?>
Tried with elif(board[guess_row][guess_col] == “X”): , same thing.

```python

Replace this line with your code.

<do not remove the three backticks above>
from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
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)
print ship_row
print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
	print "Turn", turn + 1
 	guess_row = int(raw_input("Guess Row:"))
 	guess_col = int(raw_input("Guess Col:"))
	if guess_row == ship_row and guess_col == ship_col:
  		print "Congratulations! You sunk my battleship!"
	else:
  		if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    		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"
    	    print_board(board)

Not sure why Codecademy decided to change the website a few months back. Platform doesn’t run as smoothly, gives less feedback, and not to mention the Python terminal itself is frustratingly buggy. Many times you get some random error, then type in the exact same code and it works.


#2

here:

        else:
  		if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    			print "Oops, that's not even in the ocean."
    	elif board[guess_row][guess_col] == "X":
       		print "You guessed that one already."

elif is after after else, that can’t be. elif should have the same indent level as if which checks if shot is on the board

same for the else here:

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

also wrong indent level, can’t have else after else, else is always last given it catches all remaining scenarios


#3

Code wasn’t properly formatted when I pasted it in - edited.

Are you suggesting it should be something like the following instead?

for turn in range(4):
	print "Turn", turn + 1
 	guess_row = int(raw_input("Guess Row:"))
 	guess_col = int(raw_input("Guess Col:"))
	if guess_row == ship_row and guess_col == ship_col:
  		print "Congratulations! You sunk my battleship!"
 	elif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
  		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"
    	print_board(board)

#5

no, we have:

if hit:
   print "you win"
else:
   # this else clause handles the different misses scenarios
   if on board:
   elif already
   else: missed, update board

You didn’t nest your elif properly, see this pseudo code of the program.


#6

Right…isn’t that I had in the original post?

if guess_row == ship_row and guess_col == ship_col:
  	print "Congratulations! You sunk my battleship!"
else:
  	if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    	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"
    	print_board(board)

Earlier you said you can’t have else after else - using this format how would you do it differently?


#7

no, you had:

if hit:
   print "you win"
else:
   # this else clause handles the different misses scenarios
   if on board:
elif already
else: missed, update board

where elif was after the else which handles misses, not nested inside the else clause that handles misses, that is a huge difference


#8

Ah I see your point. I think I just formatted the original post wrong, but it should be updated now.

With the code above, I am still getting the syntax error:

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

#9

which one, this one:

if guess_row == ship_row and guess_col == ship_col:
  	print "Congratulations! You sunk my battleship!"
else:
  	if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    	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"
    	print_board(board)

?
now you have:

if hit:
   print "you win"
else:
   # this else clause handles the different misses scenarios
       if on board:
   elif already guessed
   else: missed, update board

i don’t see why you gave if on board so much more indent then elif already guessed andelse: missed`

the if, elif and else which are the different missing scenarios should have the same indent level


#10

Yeah, apologies for the confusion.

This should be the original code I have that I was trying to paste into the OP.

if guess_row == ship_row and guess_col == ship_col:
  	print "Congratulations! You sunk my battleship!"
else:
  	if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    	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"
    	print_board(board)
File "python", line 37
    elif board[guess_row][guess_col] == "X":
       ^
SyntaxError: invalid syntax

#11

here:

  	if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
    	print "Oops, that's not even in the ocean."
    elif board[guess_row][guess_col] == "X":

the indent level of if and elif do not match, why? elif should always be after if or another elif


#12

they look like they are matching in my screen…


#13

well, not what you post on the forum, they do not match. I think you need re-indent some of those lines. (i had problems with indent as well in the new learning environment sometimes)


#14

Pasted the code into a local Python script and it worked after I tweaked it around a bit.

Ran a debugger on the IDE and I think the problem might’ve been that one of the indents were done with spaces rather than tab…

Couple of questions:

  • What’s the proper way of indenting something? Usually one tab seems to work, but for some reason (especially after the update) I randomly get indenting errors unless I put two tabs. Or should I use spaces?

  • Is there a way to change the course to Python 3 in settings somewhere?

edit: Also it is strange that even in the forum posts everything looks lined up on my screen but to you they are mismatched


#15

The new development could use some fixes when it comes to indent, i am sure this will be sorted out over time

if things where only that simple, no you can’t.

yep, shows as mismatch:

yea, tabs should do, but understanding soft vs hard tabs is important:

Given different editors might use different settings


#16

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