Help Needed in Python version


#1

Hi,

I am in the Python mode of code combat. I am stuck on the Final Kithmaze level. This is the code I put, but it won’t work. I’d appreciate a little help. Thanks!

enemy1= self.findNearestEnemy()
enemy2= self.findNearestEnemy()
enemy3= self.findNearestEnemy()
loop:
    self.moveRight()
    self.moveUp()
    self.moveRight()
    self.moveDown()
    self.attack(enemy1)
    self.attack(enemy2)
    self.attack(enemy3)

#2

oh, also you have to use under 12 statements.


#3

calling find nearest enemy will return the nearest enemy at the time that you call it, so enemy1 enemy2 and enemy3 will all equal whatever enemy was nearest at the start of the level. Is this what you intended?


#4

I’m guessing that you meant this code?

enemy1= self.findNearestEnemy()
enemy2= self.findNearestEnemy()
enemy3= self.findNearestEnemy()
loop:
    self.moveRight()
    self.moveUp()
    self.moveRight()
    self.moveDown()
    self.attack(enemy1)
    self.attack(enemy2)
    self.attack(enemy3)

(hint: use 3 backticks (the ` on the ~ key) around code to make it format nicely, especially when the indentation matters)

If that’s the case, you’re telling your hero to make all those moves, attack all three of those variable thangs at the end of the moving, and then do it all over again starting with the moves.
Also, @Yaur is right that all three of your enemy variables will be the same thang.


#8

if i wanted the hero to go:

self.moveRight()
self.moveUp()
self.moveRight()
self.findNearestEnemy()
self.moveDown()

in a loop, how would I do that?


#9
loop:
    self.moveRight()
    self.moveUp()
    self.moveRight()
    self.findNearestEnemy()
    self.moveDown()

but self.findNearestEnemy() does nothing if you aren’t setting a variable to it or using it for something. fyi


#10

To put something inside the loop in Python you have to indent it.
So…

loop:
    self.moveRight()
    self.say("Look at me walking!")
self.say("Not walking now!")

The two indented lines are inside the loop and will be executed over and over in sequence. The last line actually never gets executed because it is outside of the loop and the code never comes out of the loop. Don’t worry about that too much as it will be explained later. Just know that Python determines what’s inside a loop by how far it’s indented. Everything has to line up.


#11

Thanks everyone for your help. I finally got it! :slight_smile: