So I have this function in a file test.py with the following code:
def some_function():
for i in range(1, 6):
# Create 5 variables s1, s2, etc with values 1, 2 ...
exec("s{} = i".format(i, i))
print(s1)
But it gives me the error message:
File "/some_directory/test.py", line 5, in test
print(s1)
NameError: name 's1' is not defined
When I run the same code in the interpreter console, however, there is no error whatsoever, and all the variables s1, s2, s3.. are defined.
Why does the code not work when it's encapsulated?