how do I make this print 1,2,3,4 (I know I can make a loop and type print(loop) but I need it done in this specific way where the variable printed changes)
i1 = 1
i2 = 2
i3 = 3
i4 = 4
for loop in range(4):
print(i+loop)
how do I make this print 1,2,3,4 (I know I can make a loop and type print(loop) but I need it done in this specific way where the variable printed changes)
i1 = 1
i2 = 2
i3 = 3
i4 = 4
for loop in range(4):
print(i+loop)
I would advise against ever doing this, but you could implement it this way:
i1 = 1
i2 = 2
i3 = 3
i4 = 4
for loop in range(1,5):
eval("i"+str(loop))
You can't access variables that way. You can use a dictionary, though, to achieve the effect you're looking for:
d = {'i1': 1,
'i2': 2,
'i3': 3,
'i4': 4}
for loop in range(1, 5):
print(d[f'i{loop}'])
You could loop through the variables:
for value in (i1,i2,i3,i4): print(value)