13- Not again!


#1

Continuing the discussion from Not again:





Hi everyone,
I keep receiving an error message that my syntax is off on line 37, but I don't understand why, or how to fix it. Can anyone see where I'm going wrong?


Replace this line with your code.


#2

Please post your code your photo isn't clear.


#3

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"
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 "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 print_board(board)


#4

Note that the code you just posted is not equivalent to what is in your screenshot, it will not produce the same result when someone tries to run it. Take care to keep code that you post intact!

You've got a line number in that error message, telling you where your code stops making sense. Go read the code at that place and consider what it is you are doing there and how that is supposed to be written. If you can't identify the problem, then try to reduce the problem to less code, remove parts that isn't causing the problem until you have the minimum amount of code that causes that syntax error. This way you've narrowed down what the issue can be by a lot and now you can look up how that thing you're trying to do in that piece of code, is supposed to be written.

Syntax errors are tricky if you don't know what things should look like, so you have to get methodical about it, narrow it down, get something that you can look up by googling.

This is definitely bit of a hurdle, but it's something that becomes trivial with experience. You just need to build that experience. So reduce the problem to a minimum version and see if you get anywhere with that.


#5

Hi @nn210,

I think the error is indeed a syntax error, having to do with the indentation. Since @ionatan is right in that we should practise debugging our own codes, let me just give you a hint: don't you find that typing an else statement, then another if statement afterwards on the same indentation level a bit odd?

Good luck :slight_smile:


#6

@umopapsidn
I think an example is required to make it clear what you mean.

if condition:
    do stuff
else:
    do stuff
elif condition:
    do stuff

And there we have a minimal version of the issue, albeit not in actual Python.
One way to identify the problem is to read out loud the above, convert it to English, so like..

If a ball is blue, then paint it red, otherwise paint it yellow otherwise, if it's cyan, paint it green.

And your brain might now be able to clearly tell what is wrong with the above "sentence", because it isn't quite making sense. And this becomes no less obvious when written in code, with experience, by being more fluent with programming, knowing what things should look/sound like, same as you can identify a broken sentence in human language in an almost unconscious way, it's probably hard-wired in your brain in one way or another.


#7

@umopapsidn @ionatan @bandit I've fixed it, thank you for your help!

@ionatan thanks for your example, helped to make things clear


#8

the elif statements that follow the else: are the ones that are causing the error. structure such that after the else: there is an if --- : then the elif ---: also make sure that you use the correct indentation so that the statements are nested within the else: statement.