I need help with Python

Here is my code,

loop:
    coins = self.findItems()
    if self.gold < 20:
        if coins.value == 3:
            self.moveXY(coins.pos.x,coins.pos.y)

What am I missing? I have tried multiple different ways of writing this. All I want is to only pick-up gold coins but once I have 20 gold I want it to perform a different function.

self.findItems returns a list. As such, your hero is trying to move to all gold coins at once. So in between if self.gold and if coins.value you need to put a for loop:

if self.gold < 20:
    for coin in coins:
        if coins.value == 3:
            #code

Also, I recommend you replace < with <=

2 Likes

[quote=“DevinIV1, post:2, topic:5483”]
Also, I recommend you replace < with <=
[/quote]I wouldn’t. If you do, the hero will keep collecting until he has 21 gold instead of only 20. Good explanation, though!

No, the or equal to will allow it to break from the “if” when it’s at 20 also, not just at 21. <= means less than or equal to, so it will collect until 20 or more.

That would work, except that the code is not to break the loop, but to collect coins. It would instruct the hero to keep collecting while he has 20 or fewer gold.

.

I said break the “If” statement

… that’s the purpose of the program.
Edit: Oh wait… I see what you mean =P