Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Why is this condition returning False? [closed]

$
0
0

I am recreating TicTacToe in Tkinter Python and came across a bug, that I was unable to fix. In my program, I am checking if the coordinates of a click are in the TicTacToe grid and even when they are, my program returns that they are not.

class Game():    def __init__(self) -> None:        self.gridspaces_dict = {(0, 0): None, (0, 1): None, (0, 2): None, (1, 1): None, (1, 2): None, (1, 3): None, (2, 1): None, (2, 2): None, (2, 3): None}        self.cords_to_grid_cords_dict = {(0, 0): (150, 150, 316, 316), (0, 1): (316, 150, 482, 316), (0, 2): (482, 150, 650, 316), (1, 0): (150, 316, 316, 482), (1, 1): (316, 316, 482, 482), (1, 2): (482, 316, 650, 482), (2, 0): (150, 482, 316, 650), (2, 1): (316, 482, 482, 650), (2, 2): (482, 482, 650, 650)}    def add_object(self, grid_cords) -> None:        if grid_cords in self.gridspaces_dict.keys():            print("Success")        else:             raise Exception("The given Coordinates do not exist!")    def click_handler(self, coords) -> None:        self.add_object(self.get_grid_cords)    def get_cords(self, grid_cords: tuple) -> tuple:        return self.cords_to_grid_cords_dict[grid_cords]    def get_grid_cords(self, mouse_click_cords: tuple) -> tuple:        mouse_x, mouse_y = mouse_click_cords        for rectangle in self.cords_to_grid_cords_dict.values():            if mouse_x > rectangle[0] and mouse_x < rectangle[2] and mouse_y > rectangle[1] and mouse_y < rectangle[3]:                return list(self.cords_to_grid_cords_dict.keys())[list(self.cords_to_grid_cords_dict.values()).index(rectangle)]        return (-1, -1)game = Game()game.click_handler((0, 0))

This piece of code is supposed to return "Success", because obviously, (0, 0) is a key in the gridspaces_dict dictionary, so my problem is, that it does not.


Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>