0

i want to create a basic animation in python just with print() with multiple variable with all a name just like that: frame0, frame1, frame2,... I can copy paste the frame, but i want to add their on a list called frames.

That's my code:

frame0 = "D"
frame1 = "CD"
frame2 = "BCD"
frame3 = "ABCD"

frames = []
for i in range(4):
        frames.append('frame{i}')

I want to add the four variables at my list with the form frames = ["D", "CD", "BCD", "ABCD"], but here, after the loop, frames has take strings, not variables contents frames = ['frame0', 'frame1', 'frame2', 'frame3']. I know it's because i have use '' in my append but i have not found the answer of this problem. Sorry for my bad english scripting.

1 Answers1

0

If the variables frame{..} are global variables, you can do this

frame0 = "D"
frame1 = "CD"
frame2 = "BCD"
frame3 = "ABCD"

frames = []
for i in range(4):
        frames.append(locals()[f'frame{i}'])

Note if the variables are global and not local, then you can use globals()[f'frame{i}'] instead

Edit: I feel I should note that some solutions to this use eval(). I would generally recommend steering clear of that as shown by this StackOverflow answer