-1

I import a set of images of numbers (surfaces) through

for n in range(0, 10):
    globals()[f'Number{n}'] = pygame.image.load(os.path.join(f'{n}.png'))

This gives me 10 surface objects. Next i want to iterate through those 10 objects and place them somewhere on my screen using something like;

for num in range(0, 10):
    WINDOW.blit(Number[num], (pos_x, pos_y))

The Number[num] does not work. How can i dynamically call variables with the loop iteration as part of the variable name?

RAH
  • 39
  • 1
  • 8
  • Why not using a dictionary of your objects? The code will be much cleaner... – mozway May 09 '22 at 20:33
  • I have not worked with dictionaries before as i am new to Python. Would i be able to loop through a set of surfaces and store those in a dictionary as per the first loop? – RAH May 09 '22 at 20:36
  • Yes, you can loop over the keys (`for key in dic`), the values (`for val in dic.values()`), or both (`for key, val in dic.items()`) – mozway May 09 '22 at 20:38
  • Thanks, i will look into Dictionaries! – RAH May 09 '22 at 20:39
  • A simple list will handle this. `Number = []` / `for n in range(10):` / `Number.append( pygame...)`, and then `for num in Number:` / `WINDOW.blit(num, (pos_x, pos_y))`. – Tim Roberts May 09 '22 at 20:47

0 Answers0