Another level I am struggling


#1

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.

loop:
       decoysBuilt = 0
       coin = self.findNearestItem()

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

       if self.gold>=25:
          pos.x = pos.x - 5
          pos.y = pos.y
          self.buildXY("decoy", pos.x - 5, pos.y)

          pass



       if decoysBuilt >= 4:

       break

       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?


#2

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


#3

I did not really understand


#4

Put it this way:

loop:
    self.say("This statement is inside the loop!")
    self.say("This is because is it one indent forward from the loop!")
    break
    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.")

#5

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:
1)

while (not_enough_decoys):
    built decoys
self.say("here I have built enough decoys")
loop:
   if(enough_decoys)
       break
   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:

times_i_done_stuff=0
some_sort_of_loop:
   if(I_can_do_staff)
        do stuff
        times_i_done_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:

decoysBuilt++

each time you build a decoy


#6

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.


#7

You code it may look like:

loop:
    collect coins
    if enough to build a decoy
         build a decoy
    decoysBuilt++

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:

loop:
    collect coins
    if enough to build a decoy
         build a decoy
         decoysBuilt++

#8

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.


#9

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

In python it is: decoysBuilt += 1


#10

I still can’t seem to do it.


#11

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


#12

This level teaches counting

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

decosyBuilt=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.