There's a couple of issues with that code. First, on line 5 that second self.findNearestEnemy(), isn't needed and it's not actually doing anything. The main issue though is that you're always going to alternate attacking the chest even if the enemy is alive, so it's going to cause extra walking and ignoring the enemy attacking your backside. Try with an else clause so that you only attack the chest when there's no enemy.