What is this issue i keep having?


#1

I frequently have this one problem that i seem to be unable to solve…

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        distance = self.distanceTo(enemy)
        if distance > 5 :
            else:                       # this is line 15
                self.shield()

        pass
else:
    self.moveXY(40, 34)

original code for reference;

You can put one if-statement within another if-statement.

loop:
enemy = self.findNearestEnemy()

# If there is an enemy, then...
if enemy:
    # Create a distance variable with distanceTo.
    
    # If the enemy is less than 5 meters away, then attack()
    
    # Otherwise (the enemy is far away), then shield()
    
    pass

# Otherwise (there is no enemy...)
else:
    # ... then move back to the X.
    self.moveXY(40, 34)

however, on line 15 it always says i have some kind of “unexpected token”
(BTW this is on the level Stillness in Motion)

someone please help!


#2

Hello, David, and welcome. Thank you for reading the FAQ before you posted.

Is this your full code? It doesn’t appear to be so…

If it is, your indentations are a little mixed up. Your else block on line 15 should be one level of indentations back. Your final else-block must be pulled one level forward. Also, your if-statement is empty. This will break your code. You must do something within that if-statement.


#3

which if? also, it just says unexpected token. my statements appear to be full…


#4

You can’t just put an else-block inside an if-block without another if-statement on the same indentation level.
That’s why you’re getting an error on line 15.


#6

Never mind! i rechecked my if statements and fixed it!

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        distance = self.distanceTo(enemy)
        if distance < 6 :
            self.attack(enemy)
        else:                       # this is line 15
                self.shield()

        pass
    else:
    self.moveXY(40, 34)

Thanks for the help!


#7

Multiple issues:

loop:
    enemy = self.findNearestEnemy()
    if enemy:
        distance = self.distanceTo(enemy)
        if distance > 5:
            # you should do something here
            else:                # this 'else' is missing a an 'if' OR it's badly indented
                self.shield()

        pass                     # you can safely delete this line
else:                            # this 'else' is also missing an 'if' OR it's badly indented
    self.moveXY(40, 34)

Note: else always goes together with an if *

Note 2: indentation. In python indentation is important - it marks the blocks of code that “belong” to a statement.

* well, there are some advanced uses of else, like after a for or while loop in python, but that’s out of the scope here…



#8

i see…
i wasn’t doing anything on line 14 before…
and my else was inside my if…
thank you.
but my question still remains… what does unexpected token mean? (in Layman’s Terms, please)


#9

Usually it’s something that’s not in the right place and/or missing:

# example 1
if a > b           # missing colon (':')
    print a

# example 2
x = 1
else:              # missing 'if'
    y = 2

# example 3
if a < b:
    print b
    else:          # bad indentation OR missing 'if'
        y = 2

# etc.