Why does my code not assign a value to my variables? How can I get the output of A = string
and B = string
?
Here is my code:
moves = ['A', 'B']
for i in moves:
locals()[i] = 'string'
print(A, B)
EDIT:
Thank you guys for all aplies. I realised that I had made a mistake in another place in the application (yes, I'm a beginner).
I am working on a game of tic-tac-toe. I am pasting the part of the code where I used locals(). I realize there are probably simpler methods but I'm trying to code myself to learn how to think. Can you tell if there is a simpler method that can replace my code and avoid using locals()?
#Define a places
A1 = ''
A2 = ''
A3 = ''
B1 = ''
B2 = ''
B3 = ''
C1 = ''
C2 = ''
C3 = ''
#Start a game and ask a two players a moves
o_moves = []
x_moves = []
game_is_over = False
while not game_is_over:
#PLAYER 1
move1 = input("P1: Tell you move: ")
o_moves.append(move1)
for i in o_moves:
locals()[i] = 'o'
for j in x_moves:
locals()[j] = 'x'
board = f'| {A1} | {A2} | {A3} |\n-----------\n| {B1} | {B2} | {B3} |' \
f'\n-----------\n| {C1} | {C2} | {C3} |\n'
print(board)
#PLAYER 2
move2 = input("P2: Tell you pole: ")
x_moves.append(move2)
for i in o_moves:
locals()[i] = 'o'
for j in x_moves:
locals()[j] = 'x'
board= f'| {A1} | {A2} | {A3} |\n-----------\n| {B1} | {B2} | {B3} |' \
f'\n-----------\n| {C1} | {C2} | {C3} |\n'
print(board)