Help please, Python decoy drill [SOLVED]


#1

I have spent the past 2 hours on this level and I can not figure out what I am doing wrong. I have tried multiple variations of code and don’t understand why none are working.

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

the above is one sample and when I run it I just keep collecting gold and never build any decoys.

loop:
    item = self.findNearestItem()
    totalgold = self.gold + item.value
    if item:
        self.moveXY(item.pos.x, item.pos.y)
        if totalgold > 25:
            self.buildXY("decoy", self.pos.x, self.pos.y)
            d = self.decoysBuilt
               if d = 2:
               break
                  self.say("done")

the one above will build one decoy and then just stop

decoysBuilt = 0
loop:
    coin = self.findNearestItem()
    totalgold = total.gold + coin.value
    if coin:
        self.moveXY(coin.pos.x, coin.pos.y)
        if totalgold > 25:
            self.buildXY("decoy", self.pos.x, self.pos.y)
            self.decoysBuilt + 1
            if d = 1:
            else:
               break
                  self.say("done")

the above one does nothing. I am at a complete loss, please can someone tell me what I am missing and why these are all failing? I could just do it the easy way and set it to collect coins for like a minute then build 4 decoys and just have him say 4, but I really want to learn how to do this correctly


#2

You have to post your code in radiant, harmonious formatting as per FAQs so that we can understand it (like finding mistakes in indentation, etc) and help you.

Also, please always check previous posts on the same issue. You may find the solution already there. This might help


#3

Hello, Tristen. Read the FAQ, as Arunabha has said, before your next post. I’ve done the formatting for you this time, but please do so yourself in the future.

I’ll go through your third code sample, since it’s the closest to the solution.

Line 4: total gold = total.gold + coin.value. You don’t need a variable to keep track of your gold. You can just use the attribute self.gold.

Line 9: self.decoysBuilt + 1. decoysBuilt is not an attribute of self. Just use decoysBuilt. Also, just saying decoysBuilt + 1 isn’t going to do anything. Use decoysBuilt += 1.

Line 10: if d = 1. d is not defined. Did you mean it to stand for decoysBuilt? Also, use two equals signs, not one. = is for variable assignment. == is for equality comparison. Lastly, you are supposed to build four decoy, not one.

Line 11: else:. I’m not sure what this was supposed to be. Delete it. It is unneeded.

Line 13: self.say("done"). This is indented several levels of indentation too far. Move it to zero levels.

Do these things, then come back to use if you still have problems.


#4

Ok - I’m going to try to address all 3 bits of code above.

I’m assuming you do not have access to self.gold but want to solve the level anyway.

First code block:
a. You also failed to provide Totalgold with an initial value. You need to set Totalgold = 0 before the loop starts.
b. Your first mistake was trying to look at item.value AFTER you picked it up. Once your character has picked it up, the item.Value gets reset to zero (or will cause an error). So, let’s move the gold counter above the line moving to the coin.

    item = self.findNearestItem()
    Totalgold = Totalgold + item.value  
    self.moveXY(item.pos.x, item.pos.y)

c. I also don’t know why you defined the variable “D” … decoysBuilt works nicely. Replace all references to “D” with “decoysBuilt”.
d. I will admit, I’m old-school, so I’m more comfortable with an equation over the += 1 technique. You also would get an error when you went to build your second decoy as you forgot to decrease the amount of gold by the amount you just spent.

  if Totalgold > 25:
        self.buildXY("decoy", self.pos.x, self.pos.y)
        decoysBuilt = decoysBuilt + 1
        Totalgold = Totalgold - 25

e. Your test to break is wrong. Replace “if d = 1:” with “if decoysBuilt == 1”

Ok - middle set of code:
a. I’m not sure why you’re still using totalgold if you have access to self.gold. Delete this equation: totalgold = self.gold + item.value and replace “if totalgold > 25:” with “if self.gold > 25:”. This has the advantage of being updated automatically for both coins you pick up and things you spend the coins on.
b. The reason the code you put here is stopping is fairly simple. You put “if d = 2:” when you should have put “if d == 2:” Also, I would not bother with d - just use the decoysBuilt variable, making that line “if decoysBuilt == 2:”.
c. decoysBuilt is not a property of self (“self.decoysBuilt”). It is a variable. So, you either need to update it with a line like “decoysBuilt = decoysBuilt + 1” or you could do something a little trickier. There is a property of self which is “.built”. The problem is it does not return a number, it returns an array of everything you’ve built. The function len( [array] ) returns the number of elements in [array]. So, if you wanted to get a little tricky, you could do “decoysBuilt = len(self.built)”

Ok, last set of code:
Once again, I am going to assume you are solving this without access to self.gold.

a. You again forgot to start totalgold from 0. Add “totalgold = 0” before the loop.
b. totalgold is not the same as total.gold. The code probably crashes when it accesses the .gold feature of the undefined variable “total”.
c. There is no property “decoysBuilt” to the self object. If it were me, I’d replace that line “d = self.decoysBuilt” with “decoysBuilt = decoysBuilt + 1”.
d. Once again, you forgot to pay for your decoy. Add this line after the revised line in ©: "totalgold = totalgold - 25
d. The variable “d” is undefined and not needed. You also are not using the == operator (one of my most common errors). Replace this “if d = 1” with “if decoysBuilt == 4” It is also tabbed in incorrectly - it should line up with the line immediately above it. Once you do that, the “break” line is aligned correctly.
e. self.say should be aligned with loop.


#5

I’m having issues with this level crashing everytime I attempt to build a decoy. All of the WebGL goes black and crashes whenever I get to where I need to build a decoy. I’m not sure if it is an error with my code or just a PC problem? Thanks!

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


#6

It seems you have not summoned the decoys correctky


#7

Be sure to copy your code from the game and use the </> button or it won’t format properly.

PostCode

There are many people here willing and able to help. If you use the </> button correctly, your code should look like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

If it doesn’t look like the code above, you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well. Thank you.