Unsure coding for Backwoods Treasure <Python>

Hi I’m having some trouble with the level, and currently no one in my class is even at the level so I’m at the point of taking a big shot in the dark. I need to know what I’m doing wrong.
code:

loop:
    enemy=self.findEnemies()
    items=self.findItems()
    flag=self.findFlag()
    if flag:
        flagPos=flag.pos
        fy=flag.pos.y
        fx=flag.pos.x
        self.moveXY(fx, fy)
        self.pickUpFlag(flag)
        if items:
            itemPos= item.pos
            iy=item.pos.y
            ix=item.pos.x
            self.moveXY(ix, iy)
        elif enemy:
            if self.isReady("cleave"):
                self.cleave(enemy)
                self.attack(enemy)
            else:
                self.attack(enemy)
                self.attack(enemy

)

This one I’ve made thinking I should prioritize the item collection, but I don’t think it works effectively enough as well as it won’t allow my character to do somethings. any help will be great, and maybe even corrections on my part should be in order. (I put this in bugs not to report a bug in game but see where it is in my code)

Here’s what I can see off the top of my head:

  • You can just use pickUpFlag to move the flag and remove it.
  • You only check for items and an enemy if there is a flag.
  • You never define item.
  • enemy is a list. You can not attack a list.
    Fix these things, then come back.