Ask coding questions

← Back to all posts
How can i switch between vars easily?
RainBowLion1 (6)

ok i have 10 vars for example there called test_1 - 10 i want to switch between them maybe my adding or decresing a number is this posible/ whats the best way to do this?

Comments
hotnewtop
Coder100 (18137)

Use an array.

arr = [1, 2, 3]
arr[0] # 1
arr[1] # 2
arr[2] # 3

Arrays start at 0 so don't forget that

SixBeeps (5221)

This sounds like you need an array or a list. What do these variables do?

MocaCDeveloper (651)

There is no easy way to do this.

You're just gonna have to commit an action on each var separately.
Meaning, if you want to increment them all, you have to do:

test_1++;
test_2++;
test_3++;
...

If you're in Python then change the ++ to a +=1