elif raises a syntax error, but if does not
<p>I get an error when I try to change an "if" to an "elif." The code works perfectly when I'm using an if, but raises a syntax error if I try to use "elif" instead. I need to use "elif" because I only want one of the if statements to run, not both. This code works fine:</p>

<pre><code>guess_row=0
guess_col=0
ship_location=0
number_of_attempts=3

guess_location = input("Guess :").split(",")
guess_row,guess_col = int(guess_location[0]),int(guess_location[1])
if guess_row not in range(1,6):
    print("Out of range1.")
print(guess_location)
print(ship_location)         
if guess_col not in range(1,6):
    print("Out of range2.")
print(guess_location)
print(ship_location)
if ship_location == guess_location:
    print("You sunk my battleship! You win!")
else:
    print ("You missed!")
    print ("You have " + str(number_of_attempts-1) + " attempt(s) left!")
    print ("Try again!")
    number_of_attempts-=1
</code></pre>

<p>But if I change the 2nd or 3rd "if" to "elif":</p>

<pre><code>guess_row=0
guess_col=0
ship_location=0
number_of_attempts=3

guess_location = input("Guess :").split(",")
guess_row,guess_col = int(guess_location[0]),int(guess_location[1])
if guess_row not in range(1,6):
    print("Out of range1.")
print(guess_location)
print(ship_location)         
elif guess_col not in range(1,6):
    print("Out of range2.")
print(guess_location)
print(ship_location)
elif ship_location == guess_location:
    print("You sunk my battleship! You win!")
else:
    print ("You missed!")
    print ("You have " + str(number_of_attempts-1) + " attempt(s) left!")
    print ("Try again!")
    number_of_attempts-=1
</code></pre>

<p>I get a syntax error. Help?</p>

<p><code>elif</code> is not a separate statement. <code>elif</code> is an option part of an existing <code>if</code> statement.</p>

<p>As such, you can only use <code>elif</code> <em>directly</em> after an <code>if</code> block:</p>

<pre><code>if sometest:
    indented lines
    forming a block
elif anothertest:
    another block
</code></pre>

<p>In your code, however, the <code>elif</code> does <em>not</em> directly follow a block already part of the <code>if</code> statement. You have lines in between that are no longer part of the block because they are not indented to the <code>if</code> block level anymore:</p>

<pre><code>if guess_row not in range(1,6):
    print("Out of range1.")  # part of the block
print(guess_location)        # NOT part of the block, so the block ended
print(ship_location)         
elif guess_col not in range(1,6):
</code></pre>

<p>This doesn't matter to <em>separate</em> <code>if</code> statements; the un-indented <code>print()</code> statements are executed between the <code>if</code> blocks.</p>

<p>You'll need to move those <code>print()</code> functions to be run <em>after</em> the <code>if...elif...else</code> statemement:</p>

<pre><code>if guess_row not in range(1,6):
    print("Out of range1.")
elif guess_col not in range(1,6):
    print("Out of range2.")
elif ship_location == guess_location:
    print("You sunk my battleship! You win!")
else:
    print ("You missed!")
    print ("You have " + str(number_of_attempts-1) + " attempt(s) left!")
    print ("Try again!")
    number_of_attempts-=1

print(guess_location)
print(ship_location)         
</code></pre>

<p>or fix their indentation to be part of the <code>if</code> and <code>elif</code> blocks.</p>

python python-3.x