python - How to change which class variable is being called depending on user input? -


i've created class called earthquake stores relevant earthquake data (its magnitude, time hit, etc.) in class variables. want ask user data they'd see, print out data them so:

earthquake1 = earthquake()  answer = input("what data see?") print(earthquake1.answer) 

in example above, if user answers magnitude, want print out class variable earthquake1.magnitude.

is there way of doing this?

just use getattr:

earthquake1 = earthquake()  answer = input("what data see?") print(getattr(earthquake1, answer)) 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -