How do I create a counter for decoysBuilt in Sarven Desert?


#1

I’m having some trouble with this level. I can’t seem to create a counter for decoysBuilt correctly. Then I can’t exit the loop to report how many decoys I’ve built. Anyone has some ideas?


#2

Hello, Mark, and welcome.

Please post your code, what happens when you run it, and perhaps a screenshot to two of the error, if there is one. Don’t forget to read the FAQ so that you know how to format your code properly.


#3

Here is what I have…this one is saying that my code needs to be lined up on line # 18 where it says #Break out of the loop. Thanks for your help!

We are field testing a new battle unit: the decoy.

Build 4 decoys, then report the total to Naria.

decoysBuilt = 0
loop:
item = self.findNearestItem()
# Loot the coin!
if item:
pos = item.pos
x = pos.x
y = pos.y
self.moveXY(x, y)
# Each decoy costs 25 gold. Use the Quartz Sense Stone
# to know when you have more than 25 gold with self.gold.
if self.gold > 25:
self.buildXY(“decoy”, x, y)
# Keep a count of decoys you built as you go along.
decoys = decoysBuilt + 1
# Break out of the loop when you have built 4.
if decoys = 4:
break
self.say(“Done building decoys!”)

Go to Naria and say how many decoys you built.

self.moveXY(14, 36)
self.say(“Naria, I built 4 decoys!”)


#4

…I just said to read the FAQ, to learn how to format your code properly. You’ll find it under "post your code in radiant, harmonious formatting.

Anyways. To compare two values, you use two equals signs, like this:

if decoysBuilt == 4:
    break

You used only one equals sign. That is for variable assignment, not comparison

Also, you have to use your variable decoysBuilt when you tell Naria how many decoys you have.

If there is an indentation problem, then it is impossible to tell, because you have not formatted your code properly hint hint, nudge nudge. Please do so.


#5

This is what I have now…any suggestions?

loop:
    item = self.findNearestItem()
    # Loot the coin!
    if item:
        pos = item.pos
        x = pos.x
        y = pos.y
        self.moveXY(x, y)
    # Each decoy costs 25 gold. Use the Quartz Sense Stone
    # to know when you have more than 25 gold with self.gold.
    if self.gold > 25:
        self.buildXY("decoy", self.pos.x, self.pos.y)
    # Keep a count of decoys you built as you go along.
        decoysBuilt = decoysBuilt + 1
    # Break out of the loop when you have built 4.
        if decoysBuilt >= 4:
        break
self.say("Done building decoys!")
# Go to Naria and say how many decoys you built.
self.moveXY(14, 36)
self.say("Naria, I built 4 decoys!")```

#6

For future reference, formatting is done with triple backticks, on a new line, like this:

` ` `
self.say("This is how you format!")
` ` `

, without spaces between the backticks, becomes

self.say("This is how you format!")

Do that, then come back to us.


#7
decoysBuilt = 0
loop:
    item = self.findNearestItem()
    # Loot the coin!
    if item:
        pos = item.pos
        x = pos.x
        y = pos.y
        self.moveXY(x, y)
    # Each decoy costs 25 gold. Use the Quartz Sense Stone
    # to know when you have more than 25 gold with self.gold.
    if self.gold > 25:
        self.buildXY("decoy", self.pos.x, self.pos.y)
    # Keep a count of decoys you built as you go along.
        decoysBuilt = decoysBuilt + 1
    # Break out of the loop when you have built 4.
        if decoysBuilt >= 4:
            #break
self.say("Done building decoys!")
# Go to Naria and say how many decoys you built.
self.moveXY(14, 36)
self.say("Naria, I built 4 decoys!")

#8

Thank you. Now, you haven’t made all the changes I suggested you do. You still are not using decoysBuilt in telling Naria the amount of decoys you built.


#9

Ok I changed that…how do I take a screenshot using a Macbook Air?


#10

Uncomment the break command and you’re done!

You may also need to change the last line to: self.say(decoysBuilt)


Note: the break command will exit the loop (=stop it) and the code after the loop will be executed:

loop:
    # code inside the loop
    # will be repeated infinitely
    # until the following condition is true
    if condition:
        break

# code outside the loop
# will be executed after

#11

Thanks…Got it to work!