0

I am trying to make a program that makes a new variable multiple times in a function: my pseudo code:

increment = 1
"g" + increment = variable
increment = increment + 1

1 Answers1

1

You should use dictionaries:

dct = {}
increment = 1
while increment <= 10:
    dct[f'g{increment}'] = increment
    increment += 1

This will store the variables and values in a dictionary.

So you can print it like:

print(dct)

Or to get let's say the key g3, try:

print(dct['g3'])

You could also use lists to store the values, like this:

lst = []
increment = 1
while increment <= 10:
    lst += [[f'g{increment}', increment]]
    increment += 1

And then you could print the lst:

print(lst)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Excellent! Worth also calling out that `list`s are another way to store values (if the order matters more than the name). – Sean Vieira Aug 29 '21 at 01:40