Why do I get AttributeError: 'NoneType' object has no attribute 'contains'?

Viewed 358

I am fairly new to coding so this might be fairly simple. I have been stuck on this error in my code for a while. I copied the code that I think will be important. The problem occurs when I call the highlightSide function as this checks squares[i][j].contains(x, y). I figure that since it says that the 'NoneType' object, then this would mean that there is a problem in the creation of the squares in the 2d array, but I don't know for sure. If this is the problem, how should I go about fixing this because I tried a variety of possible solutions, but they all resulted in the error being conveyed in a different way?

def highlightSide(x, y):

    rows = len(squares)
    cols = len(squares[0])
    for i in range(rows):
        for j in range(cols):
            if squares[i][j].contains(x, y):
                side = squares[i][j].highlightSide(x,y)
                break
        else:
            continue
        break

def newGame():
    #random chance of who starts the new game
    playersTurn = random.randint(0, 1) > 0

    #set up the squares
    squares = np.empty((GAME_SIZE, GAME_SIZE), dtype=object)
    for i in range(GAME_SIZE):
        for j in range(GAME_SIZE):
            squares[i][j] = (createSquare(w, dotX(j), dotY(i), BOX))

#Create square from rectangle

class createSquare:

    def __init__(self, canvas,x,y,length):
        self.canvas = canvas
        self.length = length
        self.length = height
        self.bot = y + length
        self.left = x
        self.right = x + length
        self.top = y
        self.highlight = None
        self.NumSelected = 0
        self.owner = None
        #look into to make sure it work

    #to see if in the range of the square
    def contains(self, x, y):
        if y>self.bot and x < self.right and y <= self.top and y >= self.left:
            return True
        else:
            return False

def highlightSide(self, x, y):
    #calcualte the distance to each side
    dBot = self.bot - y
    dLeft = x - self.left
    dRight = self.right - x
    dTop = y - self.top

    #find which of them is the closest
    dClosest = min(dBot, dLeft, dRight, dTop)

    #make sure the color changes to highlighted color for closest if doesn't already have line
    if dClosest == dBot and not self.sideBot.selected:
        self.highlight = constants.BOT
    elif dClosest == dLeft and not self.sideLeft.selected:
        self.highlight = constants.LEFT
    elif dClosest == dRight and not self.sideRight.selected:
        self.highlight = constants.RIGHT
    elif dClosest == dTop and not self.sideTop.selected:
        self.highlight = constants.Top

    #return the highlight
    return self.highlight
0 Answers
Related