我有一个与字符串同名的变量。使用字符串,如何访问存储在同名变量中的值?
我想做以下事情:
A1=10
A2=20
var1='A1'
var2='A1+A2'
print(var1) #I want output to be 10
print(var2) #I want output to be 30
print(var1) gives 'A1'
print(var2) gives 'A1+A2'
答案 0 :(得分:-1)
A1=10
A2=20
var1='A1'
var2='A2'
print(var1) #I want output to be 10
print(var2) #I want output to be 30
print (globals()[var1] +globals()[var2])