I need help with Python


#1

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.


#2

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


#3

[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!


#4

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.


#5

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.


#6

.

I said break the “If” statement

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