setting user input to variable name [duplicate]

You can use the dictionary returned by a call to globals():

input_name = raw_input("Enter variable name:") # User enters "orange"
globals()[input_name] = 4
print(orange)

If you don’t want it defined as a global variable, you can use locals():

input_name = raw_input("Enter variable name:") # User enters "orange"
locals()[input_name] = 4
print(orange)

Leave a Comment