That was helpful kgiannakakis. I’m facing a problem as below:
a = ['zbc','2.3'] for i in range(0,5): exec('E%d=%s' %(i,a[i]))
This results in:
Traceback (most recent call last): File '', line 2, in exec('E%d=%s' %(i,a[i])) File '', line 1, in NameError: name 'zbc' is not defined
It looks like the code you’re generating expands to:
At the next iteration through the loop, you’ll get an IndexError exception because
ais only two elements long.So given the above, you are trying to assign the value of
zbctoE0. Ifzbcdoesn’t exist (which it seems that it doesn’t), then you will get the NameError you mention.It’s hard to determine what you’re actually trying to do with this code, so I’m not sure what to recommend. You could assign strings instead:
This would expand to:
You would still get the IndexError because your array
ais not 5 elements long. That should be an easy fix for you.