I’m trying to make like an ON/OFF switch. Once a variable name is entered I want it going from ‘off’ to ‘on’. There’s a lot of variables so code looks really messy if I type ‘varx = ‘on’‘ for every input
VAR1 = 'off'
VAR2 = 'off'
VAR3 = 'off'
while True:
findvar = input('Which variable do you want to turn on?: )
>> finds the variable and changes value to 'on'
I’m trying to get it so when I type var1 on the input, VAR1 goes from ‘off‘ to ‘on‘
I tried dictionary but I haven’t found to use dictionary to change variables.
Thanks!
>Solution :
Use a dictionary.
d = {
'VAR1': 'off',
'VAR2': 'off',
}
name = input('What variable do you want to turn on? ')
d[name] = 'on'