My code looks somewhat similar to this:
import os, sys
ABC = ''
XYZ = ''
FOO = ''
BAR = ''
ITEM = ''
CLEAN = ''
values = ['ABC', 'XYZ', 'FOO', 'BAR', 'ITEM', 'CLEAN']
# some code here
c = raw_input('> ')
if ' ' in c:
c = c.lower()
c = c.split()
if c[0] == 'set':
if c[1] in values:
exec("%s = '%s'" % (c[1], c[2]))
else:
# error...
# the code continues
As you may be able to tell, I am trying to assign a value to a variable, but deciding that variable based on user input and not referring to it by its name.
Basically, what I want is to assign the c[2]
string to either of the variables at the start, depending on which one the user picks in c[1]
and, in doing that, referring to those variables by the same alias (or something similar).
# Desired input
> set FOO yes
> print FOO
# Desired output
yes
# Desired input 2
> set ITEM dark
> print ITEM
# Desired output 2
> dark
I just can't come up with anything.
===========================================================================
EDIT: I edited the code to see if that would help and it didn't. It's stuck at the else condition no matter what I type with set.
EDIT 2: This is what solved my problem:
1) Converting the values
list to all strings
2) Adding .upper()
to the c[1]
Thanks to Leopold for spotting what was wrong.