1

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)
  • 1
    `locals()` is merely a way of examining the local variables. You cannot change them, or create a new one that way. – jasonharper May 24 '22 at 21:16
  • 2
    @jasonharper you actually can in global scope – Bharel May 24 '22 at 21:18
  • 2
    @jasonharper - since this is being done a module scope, `locals` is the same as `globals`. The problem is that OP tries to print both variables when only one is assigned on the first loop. – tdelaney May 24 '22 at 21:18
  • Whether `locals` changes anything is implementation dependent. In cpython it works in the global scope but not in a function. The help for locals says _NOTE: Whether or not updates to this dictionary will affect name lookups in the local scope and vice-versa is *implementation dependent* and not covered by any backwards compatibility guarantees._ – tdelaney May 24 '22 at 21:21
  • 1
    Why do you need to do this? In most situations, it is likely to be cleaner and more convenient to store the data in an ordinary dictionary rather than trying to define multiple variables in this way. – alani May 24 '22 at 21:22
  • @jasonharper Thank you for your reply. I have edited the post. Can you tell if there is a simpler method that can replace my code and avoid using locals()? – znowusiejeblem May 25 '22 at 20:44
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – MisterMiyagi May 25 '22 at 20:46
  • @MisterMiyagi Yes, I edited post. I used this method, but maybe there is a simplier way? https://stackoverflow.com/a/1373201/16470989 – znowusiejeblem May 25 '22 at 20:50

1 Answers1

2

Your code has an indentation error. B is not defined at the end of the first iteration of the loop.

To resolve, move the print() statement over one indent:

moves = ['A', 'B']

for i in moves:
    locals()[i] = 'string'
print(A, B)

This outputs:

string string
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • 2
    True, but the indentation error suggests that the OP is relatively new to Python, which also suggests that it is relatively unlikely that they have a problem that is best solved by means of `locals`. We would need more details in order to know for sure, but I suspect that some advice on the use of dictionaries might prove to be more helpful than answering the exact question as stated. – alani May 24 '22 at 21:26
  • @alani You're right I'm a beginner, but I try to write code myself and then look at solutions to avoid watching too many tutorials. Thank you for your reply. And thanks to editing my post. I have edited the post. Can you tell if there is a simpler method that can replace my code and avoid using locals() ? – znowusiejeblem May 25 '22 at 20:45
  • Keep a dictionary where the keys are the variable names and the values are one of `{'x', 'o', ''}` rather than using `locals()`. – BrokenBenchmark May 25 '22 at 21:30