Having trouble with Agrippa defense


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

#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


#3

i get unexpected token when i try and store distance

distance = self.distanceTo(enemy)


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

#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? :slight_smile:


#6

Im still confused, i moved it under if enemy and still didnt fix the problem…


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

#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


#9

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


#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)

#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:

First, ask if there are cookies.
Then ask if the cookies are chocolate chip.
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.

Hope that made sense.


#12

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


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

#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)
        if (this.isReady("cleave"))
        this.cleave();
    }
    else {
        this.attack(enemy);
    }
}

#15

you are forgetting a { after your distance check


#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 :smile:


#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. :frowning:

Help please


#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/…


#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.


#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!!