0

My code has variables mri1_tasks and mri2_tasks. Is there a function to have python interpret 'mri%d_tasks'%num (where num is equal to either 1 or 2) as the stored value of mri1_tasks or mri2_tasks based on the value of num?

G Warner
  • 1,149
  • 1
  • 12
  • 24

1 Answers1

0

Instead of having two variables, you can store the variables in a dictionary, with variable name as the key.

vars = {
    'mri1_tasks': 0,
    'mri2_tasks': 1,
}

num = 1
print vars['mri%d_tasks'%num]
jh314
  • 25,902
  • 15
  • 60
  • 80