Empty if Statement from Woodland Cleaver


#1

So I keep getting an empty if statement in Woodland Cleaver:

# Use your new "cleave" skill as often as you can.

self.moveXY(23, 23)
loop:
    enemy = self.findNearestEnemy()
    if self.isReady("cleave"):
        # Cleave the enemy!
        
    else:
        # Else (if cleave isn't ready), do your normal attack.
    self.attack(enemy)    

What the heck is going on? It says I need to put 4 spaces in front of statements within the if statement.


#2

you need to properly indent the attack statement. or properly paste your code so we can see your true indentions.

put three ` characters on a line by itself both before and after your code paste


#3

Yes, so with Python the whitespace (indentation) is very important. With the code as you’ve shown it, the attack is outside the if. To put it inside the if (in the else clause) you would add 4 spaces in front of it, so it lines up with the comment.
Does that help?


#4

Hello Eric_Conklin and welcome to this forum.
As sotonin already stated please use three backticks to format your code. For ease of answering your request, I did this for you this time. To learn more about this feature, please read the FAQ, which you should do anyway before posting.


You encountered a problem with Python. Due to the way Python works, you can not have empty statements. This is the way the language is designed and not a problem of CodeCombat.

Something is inside a code block if it is indented exactly 4 spaces relative to the container. See how everything behind the loop: is indented? All of the indented lines are inside the loop.

Now you’re getting an empty if-statement error. Let us look closely:

if self.isReady("cleave"):
    # Cleave the enemy!
    
else:
    # Else (if cleave isn't ready), do your normal attack

This is everything that belongs to the if-clause. As comments do not count as statements (the computer doesn’t even sees them when running your code), you indeed have an empty if-statement. As described earlier, Python doesn’t like this. JavaScript for example has no problem with it.

When looking at this, we can see another thing. self.attack(enemy) is not inside the else-branch. This is because self.attack(enemy) is not indented relative to else:.


The corrected code for you (without those comments) is:

self.moveXY(23, 23)                 # Not in anything
loop:
    enemy = self.findNearestEnemy() # Only inside the loop
    if self.isReady("cleave"):      # Only inside the loop
        self.cleave(enemy)          # Inside the loop and the if
    else:                           # Only inside the loop
        self.attack(enemy)          # Inside the loop and the else

#5

Thanks so much. I was having a lot of trouble finding out to paste on the forums without losing the indentation.