Davy
Davy

Reputation: 13

elif raises a syntax error, but if does not

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:

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

But if I change the 2nd or 3rd "if" to "elif":

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

I get a syntax error. Help?

Upvotes: 0

Views: 71

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124558

elif is not a separate statement. elif is an option part of an existing if statement.

As such, you can only use elif directly after an if block:

if sometest:
    indented lines
    forming a block
elif anothertest:
    another block

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

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):

This doesn't matter to separate if statements; the un-indented print() statements are executed between the if blocks.

You'll need to move those print() functions to be run after the if...elif...else statemement:

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)         

or fix their indentation to be part of the if and elif blocks.

Upvotes: 4

Related Questions