I can't get past Patrol Buster, help me please!


#1

It keeps telling me that my code is worng. Is my code wrong, and if so, what can I do to fix this problem? Do I have the wrong code, anyway? If so, cn I get a hint or a piece of the code that worked to get me started.
I am a newbie at this and I just started, so I do not know what to do. Can someone please help me?


#2

Oh yeah, and here is the code that I used to get started, and it keeps telling me that I am wrong.

self.findNearestEnemy()
self.attack(enemy1)
self.attack(enemy1)

Can you tell me why it keeps telling me to Fix My Code: enemy1 is not defined. This code has worked for almost all of my other challenges. So, again, please help me out on this challenge.


#3

Because enemy1 is not defined… How about this:

enemy1 = self.findNearestEnemy()      # see? now it's defined!
...

#4

@ant

Thanks! This should really help me out for this code. Again, than you so much! :blush: :smiley:


#5

@ant I just did what you said, it worked for the first enemy, but then it said: “self.attack(enemy1)” line 6: target is null. Is there always an enemy to attack.(use if) What is it teeling me to do then?


#6

Well, the error message here is quite right: “is there an enemy to attack? (use if)”

So you should check if there is an enemy, and only attack if there is one:

if something:       # note the colon
    do this         # note the indentation (spaces)

Cheers


#7
# Remember that enemies may not yet exist.
loop:
    enemy = self.findNearestEnemy()
    if there is an enemy:
    do this:
    self.attack(enemy1)
 For "if there is an enemy:"

It is now telling me to double check my code carefully.
I still really do not understand, can you help me with this problem @ant ?


#8

Hello, Kelsie, and wlecome. Please format your code as per the FAQ. I’ve done it for you this time, but do so yourself in the future.

You weren’t supposed to take ant’s suggestion literally. An if-statement basically checks if something is True, or if it exists. Go back to the level Gems or Death if you need to practice if-statements.


#9

Thanks! Oh, and by the way, I really appreciate how you tell other people not to give away codes, because as you said, it really ruins the thinking for the person who is asking the question or other fresh coders. Anyway, thanks!!! :relaxed: :blush:


#10

(a) “if there is an enemy” -
If there is not an enemy, what would self.findNearestEnemy() return? If there is an enemy, what does the .findNearestEnemy() method return.

(b) Please specify what language you are using.

© Assuming you are using python, the rule is that after a “:” is that you indent the next line further if it is to be executed subject to the prior line. So, you clearly have a coding error in your indention.

(d) I must admit, I do not recognize “do this:”.