python - Adding to a variable determined by a function -


i have

random.choice([clas, elve, volcan, atl, fro, dark, terr, barb]) += 1 

where want random function pick variable out of 8 listed, , want add 1 variable.

i can't assign function call error when try because can't add value random function. how make python adds 1 variable function returns?

i thinking like:

rand_return = random.choice([clas, elve, volcan, atl, fro, dark, terr, barb]) rand_return += 1 

but instead of adding value 1 of 8 variables in original list, create new variable , start messing that, rather editing variable picked random function.

you can make random.choice choose list of strings represent name of variable, , use globals() (which returns dictionary of global names, including variables) update variable , example -

rand_return = random.choice(['clas', 'elve', 'volcan', 'atl', 'fro', 'dark', 'terr', 'barb']) gbl = globals() gbl[rand_return] += 1 

shortened version -

globals()[random.choice(['clas', 'elve', 'volcan', 'atl', 'fro', 'dark', 'terr', 'barb'])] += 1 

Comments