# Having trouble with Agrippa defense

#1
``````loop:
enemy = self.distanceTo()
if enemy < 5:
self.cleave(enemy)
else:
self.attack(enemy)
``````
1 Like
#2

You are kind of mixed up there.

enemy should be set to findNearestEnemy not distanceTo

``````enemy = self.findNearestEnemy()
``````

then you need to store distance

``````distance = self.distanceTo(enemy)
``````

then

``````if distance < 5:
``````

but dont forget to check if enemy exists before getting the distance

1 Like
#3

i get unexpected token when i try and store distance

`distance = self.distanceTo(enemy)`

1 Like
#4
``````loop:
enemy = self.findNearestEnemy()
distance = self.distanceTo(enemy)
if enemy:
pass
if distance < 5:
self.cleave(enemy)
else:
self.attack(enemy)
``````
1 Like
#5

Try having `distance = self.distanceTo(enemy)` after `if enemy:` . Right now it doesnâ€™t know if thereâ€™s an enemy or not to find a distance to, so how can it find a distance to a possible enemy thatâ€™s possibly not there?

1 Like
#6

Im still confused, i moved it under if enemy and still didnt fix the problemâ€¦

1 Like
#7
``````loop:
enemy = self.findNearestEnemy()
distance = self.distanceTo(enemy)
if enemy:
pass
if distance < 5:
self.cleave(enemy)
else:
self.attack(enemy)
``````
1 Like
#8

The levels before this one were pretty easy,but i hit a brick wall hereâ€¦why no real helpâ€¦i used what has been givin so far and has not helpedâ€¦i want to keep learning but not sure what else to do

1 Like
#9

You can not call any function that passes in enemy without having it inside an if enemy block. thats your problem.

1 Like
#10

like this? my dude just stands there

``````loop:
enemy = self.findNearestEnemy()
if enemy:
pass
distance = self.distanceTo()
if distance < 5:
self.attack(enemy)
else:
self.cleave(enemy)
``````
1 Like
#11

The problem is your line `pass`

If you enter your code using the code tag it makes it easier for us to see your indentation. Iâ€™ve updated your code with the tag to help see what is happening. (If you edit the post you can see how I did it.)

You need to indent the line
`if distance < 5:`
and the others after it. You are only checking distance if there is an enemy.

Currently your code says if there is an enemy calculate the distance. Regardless of the previous answer, if the distance is under 5 attack the enemy, otherwise cleave.

To put it in another context, it is like you have written out interview questions:

If they are not chocolate chip, then they must be oatmeal raisin.

imagine you have this conversation with your mom:

You: Do we have any cookies?
Mom: No
You: Are they chocolate chip cookies?
Mom: ???

If the first answer is no, you shouldnâ€™t do anything. If the first answer is yes, then you will want to check the cookie type.

3 Likes
#12

hmm yeah maybe i need to go watch more tutorials on youtubeâ€¦thanks for trying to help me tho

1 Like
#13
``````loop:
enemy = self.findNearestEnemy()
if enemy:
distance = self.distanceTo(enemy)
if distance < 5:
self.cleave(enemy)
else:
self.attack(enemy)
``````
1 Like
#14

Any help??? I cleave just fine, but Anya just stands there while munchkin ogres spill her blood all over the meadow. I am using Java Script.

``````loop {
var enemy = this.findNearestEnemy();
if(enemy) {
var distance = this.distanceTo(enemy);
if (distance < 5)
this.cleave();
}
else {
this.attack(enemy);
}
}
``````
1 Like
#15

you are forgetting a { after your distance check

1 Like
#16

you forgot to use { } in your if commands
well in this round youâ€™ll need extreme amount of health i think? ( i have Tharin with 491HP and ended up to 61HP) so instead of attacking in my else i just shielded til my cleave is ready to use.or maybe you can just run around? i donâ€™t know. but hey i survived the round

1 Like
#17
``````loop:
enemy = self.findNearestEnemy()
if enemy:
distance = self.distanceTo(enemy)
if distance < 5:
self.cleave(enemy)
else:
self.attack(enemy)
``````

She attacks the first group fine, but the next group kills her.

1 Like
#18

Indentations, they will save your life!

You want your code too look like this:

``````loop:
# <- Note the four spaces
enemy = self.findNearestEnemy()
if enemy:
#kill him just like above (again with 4 more spaces, 8 total)
``````

With indentation you tell python what is inside and what is outside of a loop/if/â€¦

1 Like
#19

Hi J_F,

Thank you. When copying and pasting the 1st two lines it removed the indent (4 spaces)

Here is the screenshot. While sheâ€™s been hacked to death it stays highlighted on line 6.

Below is the error i get if I try to indent line 3.

1 Like
#20

I found out what it was. Her armor wasnâ€™t strong enough to handle the battle. I purchased new armor and she was fine.

I tried to shield her initially, but that didnâ€™t work. Thanks for the help!!

1 Like