-1

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.

pwn'cat
  • 111
  • 1
  • 6
  • your if condition `if c[1] == str(val)` will never succeed as all the elements in list are empty – mchackam Jan 23 '16 at 07:05
  • Yes, `values` is just a list of empty strings. The actually names of the variables are not available to you. – Tom Karzes Jan 23 '16 at 07:06
  • I can see you're trying to do "variable variables," which is rarely the correct course of action for experienced Python programmers and never the correct course of action for inexperienced ones. It's hacky and slow and fragile, and totally unnecessary 99.9% of the time (at least). – TigerhawkT3 Jan 23 '16 at 07:22

1 Answers1

0

You have three different bugs.

  1. values is a list of the values of the variables, and not of their names. So they're all equal to ''. You want a list of the variable names, like so:

    values = ["ABC", "XYZ", "FOO", "BAR", "ITEM", "CLEAN"]
    
  2. The names of the variables are uppercase, but you convert the command string to lowercase. If you input set ITEM dark, it will convert to ['set', 'item', 'dark'], and since 'item' != 'ITEM', it won't find a marching value to set. You should skip this line

    c = c.lower()
    

    and instead only make the command name case-insensitive

    if c[0].lower() == 'set':
    
  3. val + " = %s" % c[2] results in ITEM = dark, so it will try to set the value of variable ITEM to the value of variable dark, but this variable does not exist. You want to set it to the string "dark", so you need quotes: val + " = '%s'" % c[2]

Finally: This is a strange thing to do, why are you doing it? For most use cases I can think of, you'd be better served by a dictionary, like so:

userSetVariables = dict( (k,'') for k in ["ABC", "XYZ", "FOO", "BAR", "ITEM", "CLEAN"] )

c = raw_input('> ')
if ' ' in c:
    c = c.split()
    if c[0].lower() == 'set' and userSetVariables.has_key(c[1]):
        userSetVariables[c[1]] = c[2]

This results in:

> set ITEM dark
>>> print userSetVariables["ITEM"]
dark