0

I'm trying to get the user to specify two variables, then change the last one to the first one.

print('Whats your move?')
move = raw_input('>>> ')
move = move.split()
move[1] = move[0]
move[0] = ' '

Sort of like that code, but instead of just changing the list, I want to actualy change the variables.

So if someone did inputed f7 f6 it would change the variable f6 to the same value at f7, and then turn f7 to whitespace.

Is there a way to do this in python 2.7?

  • 2
    Any time you are thinking of dynamically accessing variables by name in Python you should rather be thinking about arrays and/or dictionaries instead. For example, given `board` that is a `dict`, `board[move[1]] = board[move[0]]; board[move[0]] = ' '` – Amadan Sep 27 '18 at 10:05
  • 1
    This looks like an XY problem. Also related: https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – timgeb Sep 27 '18 at 10:05
  • Maybe unpacking `a, b = move.split()` can help? – Evgeny Sep 27 '18 at 10:05
  • @EPo I think OP actually wants to inject names into the global scope, depending on what the user put in. – timgeb Sep 27 '18 at 10:07
  • 1
    It's *possible*, but in general, doing that sort of thing is a really bad idea. Instead, make `f6` and `f7` keys of a dict, and then it's really easy, eg `old, new = 'f7', 'f6'; my_dict[new] = my_dict[old]` – PM 2Ring Sep 27 '18 at 10:21

0 Answers0