Please Help with Sarven Sentry

I dont know what is wrong but my character places a fence down after he places down the mine with black.

loop:
    
    flag = self.findFlag()
    if flag:
        if flag.color is "black":
            flagPos = flag.pos
            flagX = flagPos.x
            flagY = flagPos.y
            self.buildXY("fire-trap", flagX, flagY)
    if flag:
        if flag.color is "green":
        flagPos = flag.pos
        flagX = flagPos.x
        flagY = flagPos.y
        self.buildXY("fence", flagX, flagY)
    else:
        item = self.findNearestItem()
        if item:
            itemPos = item.pos
            itemX = itemPos.x
            itemY = itemPos.y
            self.moveXY(itemX, itemY)

Please Help Me!

Please read the FAQ before you post again. You need to learn how to format you code properly. I’ve done so for you this time, but you must learn how to do it yourself.

You need to indent the code you use if the flag color is “green”.