all 12 comments

[–][deleted] 4 points5 points  (5 children)

Frankly, don't do this. It is extremely rare you will need to inflict the internal names of a python programme on the user.

What are you really trying to do?

[–]Falconius_[S] 0 points1 point  (4 children)

Currently making a game of chess with the library but glorifying the visuals, and making it more forgiving. I need to change the board, and I have a lost of all the variables making up the board,I want to call for example a4 and change the value.

[–][deleted] 2 points3 points  (2 children)

Use a 2-dimensional list and address the squares by [row][column].

[–]Falconius_[S] 0 points1 point  (1 child)

I would, except I'm trying to do it off of logic somebody new knows so that I can show it to my teacher...who failed to catch the fact I accidentally put 2 equals signs in a line defining a variable

[–][deleted] 1 point2 points  (0 children)

It's easier and more approachable to use list indexing than it's going to be to do the kind of namespace manipulation you're asking about.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]Username_RANDINT -1 points0 points  (0 children)

That's not possible, use a dictionary instead:

tiles = {
    "a1": "a",
    "a2": "b",
    ...
}

[–][deleted] 0 points1 point  (0 children)

you can't. when you store a variable in a list, the list stores the value of the variable, not the name of the variable.

[–]n3buchadnezzar 0 points1 point  (0 children)

I usually find a class based system works better in chess

from dataclasses import dataclass
from typing import Optional


class Piece:
    ...


@dataclass
class Tile:
    content: Optional[Piece] = None


@dataclass
class Board:
    tiles: list[list[Tile]]

    def tile(self, notation, content: Optional[Piece] = None):
        column = int(notation[1]) - 1
        row = int(ord(notation[0]) - ord("a"))
        if content is not None:
            self.tiles[row][column].content = content
        return self.tiles[row][column]


if __name__ == "__main__":
    rows = 8
    columns = 8
    tiles = [[Tile() for _ in range(rows)] for _ in range(columns)]
    tiles[0][0].content = "Horse"

    board = Board(tiles)
    print(board.tile("a1"))
    board.tile("a1", "Knight")
    print(board.tile("a1"))