Bait and switch (Python)

I don’t really know what’s wrong with my code. The hero moves to catch the first coin and then stops, instead of keeping collecting coins until it reaches 25. Please help me to understand how to fix it. Thank you.

def collectUntil(enoughGold):
# While hero.gold is less than enoughGold:
coin = hero.findNearestItem()
while hero.gold < enoughGold:
hero.moveXY(coin.pos.x, coin.pos.y)
pass

Collect 25 gold for one decoy and build it on the red mark.

collectUntil(25)
hero.buildXY(“decoy”, 40, 52)

It’s better to hide.

hero.moveXY(20, 52)

Use the collectUntil function to collect 50 gold:

collectUntil(50)

Build a “decoy” on the bone mark:

hero.buildXY(“decoy”, 68, 22)

Build a “decoy” on the wooden mark:

hero.buildXY(“decoy”, 30, 20)

Hi please format your code so I can read it more easily:

Thanks
Danny

def collectUntil(enoughGold):
# While hero.gold is less than enoughGold:
coin = hero.findNearestItem()
while hero.gold < enoughGold:
hero.moveXY(coin.pos.x, coin.pos.y)
pass

#Collect 25 gold for one decoy and build it on the red mark.
collectUntil(25)
hero.buildXY(“decoy”, 40, 52)

#It’s better to hide.
hero.moveXY(20, 52)

#Use the collectUntil function to collect 50 gold:
collectUntil(50)

#Build a “decoy” on the bone mark:
hero.buildXY(“decoy”, 68, 22)

#Build a “decoy” on the wooden mark:
hero.buildXY(“decoy”, 30, 20)

Define the coin variable inside the while hero.gold < enoughGold: statement
like below

while hero.gold < enoughGold:
coin = hero.findNearestItem()

Everything else is correct. The reason why is because if the coin = hero.findNearestItem() is not inside the while statement it will always be the first coin position

It works…! Like this it sounds so easy. I really appreciate your help, thanks a lot.

1 Like

I am having trouble on the same level. here is my code:

def collectUntil(enoughGold):
    # While hero.gold is less than enoughGold:
    coin = hero.findNearestItem()
    while hero.gold < enoughGold:
        coin = hero.findNearestItem()
pass
#Collect 25 gold for one decoy and build it on the red mark.
collectUntil(25)
hero.buildXY("decoy", 40, 52)

#It’s better to hide.
hero.moveXY(20, 52)

#Use the collectUntil function to collect 50 gold:
collectUntil(50)

#Build a “decoy” on the bone mark:
hero.buildXY("decoy", 68, 22)

#Build a “decoy” on the wooden mark:
hero.buildXY("decoy", 30, 20)

you have to move to the coin. You have already defined it and know what it is, so use this method:

hero.moveXY(coin.pos.x, coin.pos.y)

can someone help me with my code it says this when i try to build a fire trap

def collectUntil(enoughGold):
    # While hero.gold is less than enoughGold:
    coin = hero.findNearestItem()
    while hero.gold < enoughGold:
        coin = hero.findNearestItem()
        hero.moveXY(coin.pos.x, coin.pos.y)
pass
#Collect 25 gold for one decoy and build it on the red mark.
collectUntil(25)
hero.buildXY("decoy", 40, 52)

#It’s better to hide.
hero.moveXY(20, 52)

#Use the collectUntil function to collect 50 gold:
collectUntil(50)

#Build a “decoy” on the bone mark:
hero.buildXY("decoy", 68, 22)

#Build a “decoy” on the wooden mark:
hero.buildXY("decoy", 30, 20)

nvm fixed
i was using the wrong tool