Rich Forager using Python[SOLVED]

Hello, I have been stuck on this level for a few days now… I was wondering if you could point out any mistakes that would help me?

while True:
    flag = self.findFlag()
    enemy = self.findNearestEnemy()
    item = self.findNearestItem()

if flag:
    self.pickUpFlag(flag)
elif enemy:
    if self.isReady("cleave"):
        self.cleave(enemy)
    else:
        hero.attack(enemy)
    if item:
        itemPOS = item.pos
        itemx = itempos.x
        itemy = itempos.y
        self.moveXY(itemX, itemY)

1 Like

Hey there, welcome to the CodeCombat discourse!
With your indentation of the if-statement block, you’ll only check for and move to items, while there is an enemy!
Preferable change it to elif and move it further left, out of the elif enemy block. in addition to that, you have declared (not sure if that’s important for python) the item variables once with split uppercase/lowercase and then used it differently after that.
Hope it helped :sweat_smile:
If there are further issues (or not-solved ones with what i think was a potential problem), please let us know more about the issue you’re having!

Edit:
Also: increase the spacing on the left for every line after
item = self.findNearestItem()
If i’m correct, it’s probably not running because it isn’t considered in the while True: loop.

3 Likes

Thank you so much bro! :upside_down_face:

1 Like

I am having a problem as well, I can’t seem to figure out how to get the items. this is my code can you tell me the problems?

while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    if flag:
        # What happens when I find a flag?
        hero.pickUpFlag(flag)
    elif enemy:
        # What happens when I find an enemy?
        hero.pickUpFlag(flag)
        hero.attack(enemy)
    elif item:
        # What happens when I find an item?
        if item:
            item = self.findNearestItem()
            itemPOS = item.pos
            itemx = item.pos.x
            itemy = item.pos.y
            self.moveXY(itemX, itemY)

Hi @DanTDM_4,

itemX & itemY are different from itemx and itemy. Remember python is case-sensitive!
Danny

thanks for the help DP

1 Like