I don't know a matching title for this question..

Hi,

d0 = 4
d1 = 6
d2 = 4
d3 = 0

number = '2' 


‘number’ is a variable, d0-d3 are not variables.
How to get the value of d(number); or in this case the value of d2 so I would get 4?
Thanks in advance

hi!

print(eval("d"+number))

Thanks a lot!

I think you actually want a list:


d = [4,6,4,0]
print(d[number])

if number is zero, it will print 4. If it’s 1, it will print 6 etc…

what sdfgeoff said, I use this method all the time

thing = thingList[own[‘index’]]

this way you can cycle through a list easily using a property,
(like targets or inventory or abilities etc)

You can do this many ways, such as a dictionary


data = {'d1': 1, 'd2': 2}

i = 1
value = data["d{}".format(i)]

print(value) # prints d1 (1)

However, in your original post:
d1, d2, d3, d4 and number are all variables.

Yes a dictionary would be best here.

In my case using eval(…) like youle said is the easiest.

Hey thanks, I’ve never looked at eval or exec before. After reading about them I think they could be very useful in a few situations where I was having problems…

that is nifty, so it makes a string, and then looks up if the string exists?

if it does not exist, will it throw a error? or print nothing?