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