Another level I am struggling


Hey guys. This is another level I am having a hard time going.
It is called decoy drill. I know you have to break the loop in order for the computer to follow the code on the bottom. Somehow, it did not seem to. It only did the code on what the break is on top of. Here is my code first.

       decoysBuilt = 0
       coin = self.findNearestItem()

       if coin:
          pos = coin.pos
          x = pos.x
          y = pos.y
          self.moveXY(x, y)

          pos.x = pos.x - 5
          pos.y = pos.y
          self.buildXY("decoy", pos.x - 5, pos.y)


       if decoysBuilt >= 4:


       self.moveXY(14, 36)
       self.say("done building decoys!")

This makes sense to me. Somehow, the last two pieces of code are not done. Can you tell me why?


That is because those pieces are inside the loop. Once you break out, it looks for things outside the loop, or its indentation level.


I did not really understand


Put it this way:

    self.say("This statement is inside the loop!")
    self.say("This is because is it one indent forward from the loop!")
    self.say("This statement never gets reached :(")
    self.say("This is because once *break* is found, the program immediately exits the loop.")
    self.say("Therefore, the statements after the *break* never get run.")
self.say("This statement, however, is not inside the loop. ")
self.say("I get run after *break* is reached.")


What you want to implement is a while loop. As the name it say, it will do thing for a while and then exit the loop.

There are two ways to implement it:

while (not_enough_decoys):
    built decoys
self.say("here I have built enough decoys")
   build decoys
self.say("here i built enough decoys")

The first way is recommended because it is more clearer and less prone to errors.


Second, you need to implement counting code. Again is a standard way:

        do stuff

Pay attention to the times_i_done_stuff++ It is very important it adds +1 to the value of times_I_done_stuff. For your level you need to do:


each time you build a decoy


Now I have the total opposite. The code after the break happened, but it happened at the start too fast. I want it to happen when I build 4 or more decoys. However, he did it at the start of the game.


You code it may look like:

    collect coins
    if enough to build a decoy
         build a decoy

Here the increment (decoysBuilt++) is not at the same level as the build decoy code. As a result, you’ll add to the number of decoys built even if you are not building one.

It should be like:

    collect coins
    if enough to build a decoy
         build a decoy


I still don’t get the decoysBuilt++
In codecombat, I did not learn that yet.
I do not understand. I full did it, but I guess I was missing something.


decoysBuilt++ is how most non-python languages write add 1

In python it is: decoysBuilt += 1


I still can’t seem to do it.


I used flags to solve it
made him get enough then black flag to build four decoy a and violet to say and green to move


This level teaches counting

How your code will know how many time it has done something ?
1 start with 0


2 each time you do something add one.

build a decoy
decoysBuilt += 1

It still looks a bit weird? That’s because we need write:

decoysBuilt = decoysBuilt + 1

Or in old english:
make what I have be that I was already given and one more

But it takes too much time to write decoysBuilt twice.

+=1 is the short form of the above line.