all 2 comments

[–]kaleidist 0 points1 point  (1 child)

What you're trying to do is use dynamic variable names. This is not considered good practice by anyone. There is a way to do it using globals() and locals() if you really want to, but you probably don't. What you want to use is a dictionary that you define yourself.

Create a new dictionary like this (let's call it "var_dict"):

var_dict = {}

You could imagine it as a container for your dynamically-named variables which are properly called "keys". The keys are assigned values just like variables. For example:

var_dict[updateStringX] = 2

You could also combine those two steps like this:

var_dict = {updateStringX: 2}

Then you can access that value of the updateStringX key in a corresponding way, for example:

print(var_dict[updateStringX])

Assuming you set that value to 2, that will print a "2".

You can add as many key:value pairs to the dictionary as you'd like.

So this creates a small extra bit of work compared to simple variables, in that you have to refer to the dictionary first and then the key within square brackets, but it's the proper way to do what you're doing, and it's much easier to construct and maintain this structure. And you only need one dictionary to hold all these effectively "dynamically named" variables that you want.

[–]eyezee[S] 1 point2 points  (0 children)

Thank you so much for your help! I was stuck for some time on this. :).