Search code examples
pythonpython-3.xstring-formattingtic-tac-toe

Why does string-formatted global variable remain the same after changing the variables in the .format?


New to python, trying to code a game of tic tac toe and ended up in this situation, where I cannot seem to print out a version of my table (multi-line string) with the new O assigned to the position from the input, even after assigning O to the variable at the position.

the code below is incomplete, just focusing on the issue above for now.

 mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
table = '       |       |       \n' \
        '   {}   |   {}   |   {}   \n' \
        '_______|_______|_______\n' \
        '       |       |       \n' \
        '   {}   |   {}   |   {}   \n' \
        '_______|_______|_______\n' \
        '       |       |       \n' \
        '   {}   |   {}   |   {}   \n' \
        '       |       |     '.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3'])

def assign(i):
    if q%2==0:
        mydict[str(i)]='O'
        print(mydict)
    else:
        mydict[str(i)]='X'

q=2
while q%2==0:
    x=input('Player 1, please choose your position:')
    assign(x)
    print(table)

When I run:

TIC TAC TOE
       |       |       
       |       |       
_______|_______|_______
       |       |       
       |       |       
_______|_______|_______
       |       |       
       |       |       
       |       |       
Player 1, please choose your position:a1
{'a1': 'O', 'a2': ' ', 'a3': ' ', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'c1': ' ', 'c2': ' ', 'c3': ' '}
       |       |       
       |       |       
_______|_______|_______
       |       |       
       |       |       
_______|_______|_______
       |       |       
       |       |       
       |       |       
Player 1, please choose your position:

As you can see, the O does not appear in position a1.


Solution

  • Modifying mydict isn't enough, also you need some other changes:

    mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
    def table():
        table = '       |       |       \n' \
                '   {}   |   {}   |   {}   \n' \
                '_______|_______|_______\n' \
                '       |       |       \n' \
                '   {}   |   {}   |   {}   \n' \
                '_______|_______|_______\n' \
                '       |       |       \n' \
                '   {}   |   {}   |   {}   \n' \
                '       |       |     '.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3'])
        return table
    def assign(i, q):
        if q%2 == 0:
            mydict[str(i)]='O'
            print(mydict)
        else:
            mydict[str(i)]='X'
        print(table())
    
    q=0
    while ' ' in mydict.values():
        x=input('Player %s, please choose your position:' % ((q % 2) + 1))
        q += 1
        assign(x, q)