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

getting string of a variable as an output

how can I get my output to be the string of a name variable, if the input is equal to the name variable, I want the string of the variable to print. Thanks in advance.

    a = 'My name'
    b = 2
    c = 3
    print('input any alphabet of your choice below:')
    print('Enter your name below')
    print('input below')
    running = True
    while running:
      if user_input == a:
         print('This is:', a)
         running = False
      else:
         print('wrong input')

>Solution :

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

Use a dictionary instead of named variables, so that you can use the input as a key in the dictionary:

my_vars = {
    'a': 'My name',
    'b': 2,
    'c': 3,
}

while True:
    try:
        print(f"This is: {my_vars[input()]}")
        break
    except KeyError:
        print('wrong input')
x
wrong input
b
This is: 2
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