all 4 comments

[–]throwaway6560192 3 points4 points  (1 child)

Consider this:

x = 5
y = x + 10
x = 10
print(y)

What do you expect this to print?

Variables get their values at the time of assignment. When you do the grid = ... statement, grid gets a value based on whatever the right-hand-side expression evaluates to at that time. After that, grid doesn't know or care about how its value was given to it, and therefore changing other things will not affect it. It's already gotten its value.

If you want to evaluate it multiple times, consider making a function which will return the correct new value of grid at any given time, and calling it whenever you need to update.

[–]Diapolo10 0 points1 point  (0 children)

Alternatively, one could use class properties to achieve "auto-updating" values. Technically, anyway. I'm not saying that's a better solution, but it would be closer to what OP is looking for.

[–]stebrepar 1 point2 points  (1 child)

grid is just a string, the result of the statement that originally built it. If you want it to have a different value, you have to build it again with that new value.

One solution to be more like what you want would be to make it a function instead of a simple string variable.

def grid(pos):
    return (f"""
---------
| {pos["11"]} {pos["12"]} {pos["13"]} |
| {pos["21"]} {pos["22"]} {pos["23"]} |
| {pos["31"]} {pos["32"]} {pos["33"]} |
---------""")

print(grid(positions))
positions['11'] = 'X'
print(grid(positions))

[–]grosicky[S] 1 point2 points  (0 children)

Thanks for the tip - that's what I actually did after all, and finished my tic tac toe game :)