Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Help with Decoy Drill

decoysBuilt = 0
while True:
    coin = hero.findNearestItem()
    
    if coin:
        # Collect the coin!
        hero.moveXY(coin.pos.x, coin.pos.y);
        pass
    # Each decoy costs 25 gold.
    # If hero.gold is greater than or equal to 25:
    if hero.gold >= 25:
        # buildXY a "decoy"
        hero.buildXY("decoy", 36, 30)
        # Add 1 to the decoysBuilt count.
        decoysBuilt == decoysBuilt + 1
        if decoysBuilt == 4:
            # Break out of the loop when you have built 4.
            break
            pass
            
hero.say("Done building decoys!")
hero.moveXY(14, 36)
# Say how many decoys you built.
hero.say("I built 4 decoys")

it keeps making more than 4 decoys and I cannot figure out how to stop it from doing so. Please help!

Dan, you defined the variable decoyBuilt for a reason. In your final statement, you are having your hero say a literal string…instead, have him say the concatenation of the variable + the words to say.

so I should have him say “hero.say(DecoysBuilt)”
instead of my origanal statement?

decoysBuilt = 0
while True:
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y);
        pass
    if hero.gold >= 25:
        hero.buildXY("decoy", 36, 30)
        decoysBuilt == decoysBuilt + 1
        if decoysBuilt == 4:
            break
            pass
            
            hero.say("Done building decoys!")
            hero.moveXY(14, 36)
            hero.say("I built 4 decoys")

I modified the code but it is not registering the if “decoysBuilt == 4: statement”.
Please help!

Actually, the original code was good, just one line needed to be changed, and yes, have him say that…only, be careful of spelling and capitalization…they count!

The new version you posted has the final statements indented so that they are now part of the preceding if block…you don’t want to indent them.

can you send me a completed code to compare with because I really need to see what is going on cause I think I fixed all of it but it still builds more than 4 decoys

Sorry, no can do. Instead, copy the code from your first post in to the game editor, replacing what you now have. Then, edit the ‘say’ statement:

# Say how many decoys you built.
hero.say("I built 4 decoys")

To match what you asked in the 4th reply on this topic…simple :slightly_smiling_face:

should this code work?

decoysBuilt = 0
while True:
    coin = hero.findNearestItem()
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y);
    if hero.gold >= 25:
        hero.buildXY("decoy", 36, 30)
        decoysBuilt = decoysBuilt + 1
        hero.say("hi" + hero.gold)
        hero.say("hello" + decoysBuilt)
        
    if decoysBuilt == 4:
        hero.moveXY(14, 36)
        hero.say("I built 4 decoys")

No…start over with the code from your original post:

decoysBuilt = 0
while True:
    coin = hero.findNearestItem()
    
    if coin:
        # Collect the coin!
        hero.moveXY(coin.pos.x, coin.pos.y);
        pass
    # Each decoy costs 25 gold.
    # If hero.gold is greater than or equal to 25:
    if hero.gold >= 25:
        # buildXY a "decoy"
        hero.buildXY("decoy", 36, 30)
        # Add 1 to the decoysBuilt count.
        decoysBuilt == decoysBuilt + 1
        if decoysBuilt == 4:
            # Break out of the loop when you have built 4.
            break
            pass
            
hero.say("Done building decoys!")
hero.moveXY(14, 36)
# Say how many decoys you built.
hero.say("I built 4 decoys") # and simply change this line

nope it works. I just tested it

try it on your code stuff and see if that works

lol…I didn’t realize the question there, was still focused on your original. But yes, it works.

Yay, glad that it works for more than 1 person!

2 Likes

It doesn’t work for me…
Code:

decoysBuilt = 0
while True:
    coin = hero.findNearestItem()
    
    if coin:
        # Collect the coin!
        hero.moveXY(coin.pos.x, coin.pos.y);
        pass
    # Each decoy costs 25 gold.
    # If hero.gold is greater than or equal to 25:
    if hero.gold >= 25:
        # buildXY a "decoy"
        hero.buildXY("decoy", 36, 30)
        # Add 1 to the decoysBuilt count.
        decoysBuilt == decoysBuilt + 1
        if decoysBuilt == 4:
            # Break out of the loop when you have built 4.
            break
            pass
            
hero.say("Done building decoys!")
hero.moveXY(14, 36)
# Say how many decoys you built.
hero.say("I built 4 decoys") # and simply change this line
2 Likes

There should be only one equal sign in decoysBuilt.

3 Likes

Oh. Okay! Thanks for the help! I passed the level!

3 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.