Thunderhooves else: error?


#1

Hello, firstly I’d like to say it might just be me forgetting something but I swear something doesnt make sense about this else: error…
This is my code in Thunderhooves

So, the else command at the very end gives me an error message and I don’t understand what’s wrong, I checked the help video and it should work…
Could someone please tell me whats wrong?


#2

The if number two and three seems to be indented one step too far. Also remove both “pass”, not sure if they’re messing anything up but they’re not serving any purpose.


#3

Alright I removed both of the ‘pass’ commands and now it works, I didn’t touch anything else. It works now so thanks for helping me out!


#4

Python lives and dies by the indent . . . So if you have things mis-indented it will not do what you probably want it to. (in this case the multiple indent was causing it to think the “if” was completely over and done with, so “else:” was an error since there was no “if” to attach it to.)

So to be safe only use single level indents.

# comment
loop:
    ...
    if yak:
        # comment
        if yak.pos...:
            self.build...
        ...
    else:
        ...

#5

When I went back to check why my error in my else was wrong I checked the help video and the way I did it works.

yak = self.findNearestEnemy
loop:
       if yak:
          if yak.pos.y > self.pos.y
                self.buildXY("fence", yak.pos.x, yak.pos.y - 10)
          if yak.pos.y < self.pos.y
                self.buildXY("fence", yak.pos.x, yak.pos.y + 10)

       else:
            self.moveXY(self.pos.x, self.pos.y + 10)

I dont understand what you mean by "in this case the multiple indent was causing it to think the ‘if’ was completely over and done with, so ‘else:’ was an error since there was no ‘if’ attach to it."
I don’t understand what you mean because the else: is attached with the very first if and they were both same indentation, everything was lined up properly… ( I always perfect my indentions before making a forum post so I don’t waste peoples time.) Also, the other 2 if’s are lined up after the first if so basically IF there was a yak AND IF the yak’s Y position coordinates are greater then MY Y Coordinates, then self.buildXY(the yaks x position, the yaks y position - 10 units ) but IF the yak’s pos is less then my pos, self.buildXY(the yaks x pos, the yaks y pos + 10 units)
ELSE (NO YAK)
Walk right.

(That was just a more easier way to see it in my opinion to see why the else would work. Sorry if you don’t understand what I’m trying to say out of that) ( I also kinda don’t know what you mean because I removed the 2 pass’s (someone suggested to do that) and it worked just fine.) Basically the else would still be lined up with the very first if saying if there was a yak
if yak : build walls
else: walk?

(That’s my simple way of showing it)


#6

The code you posted now is perfectly correct but if you go back and look at the picture in your first post I think you can see the double indentation after the first if (after the comments). Moving back those indentations to the correct position would have fixed the problem even without removing the passes.