I need help with "drop the flag"

Ok so i got the code, and it works, but instead of placing a fire trap where i put the flag, he just picks the flag up and continues with collecting the coins. heres my code:

while True:
    flag = self.findFlag()
    if flag:
       if flag.colour is "black":
           self.buildXY("fire-trap", flag.pos.x, flag.pos.y)
        self.pickUpFlag(flag)
        
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)

If anyone can tell me where i went wrong it would be much appreciated.

Hello, Rhys, and welcome. Please read the FAQ before you post again, to learn how to properly format you code. I have done it for you this time, but do so yourself in the future.

Perhaps you aren’t using a black flag? That might explain it.

The flag,colour is alien to me. I recommend:

flagBlack=self.findFlag("black")
if flagBlack:
    self.pickUpFlag(flagBlack)
    self.buildXY("fire-trap",self.pos.x,self.pos.y)

Alright now there appears to be something wrong with “else” It says “please double check your code carefully” any suggestions?

OK yes finally i have got it to work. Thank you for all your suggestions it really helped!