Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find and change a variable's value through input

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

Use a dictionary.

d = {
    'VAR1': 'off',
    'VAR2': 'off',
}

name = input('What variable do you want to turn on? ')
d[name] = 'on'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading