Widespread Augmented Reality

Widespread Augmented Reality
Click on the image to get the Android Augmented Reality Heads up Display

Friday, August 5, 2022

Python Dynamic Numbered Variables in Loop

I am probably doing this in the most inefficient way possible, but the code is simply to demonstrate how to run through a counter and add the number incrementally on to the end of a variable. For instance I just want to add a number at the end of string "a" and read the variable "a1" through "a4".

#Putting string literal of numbers into an array separated by spaces
a1 = "3".split(" ")
a2 = "7 4".split(" ")
a3 = "2 4 6".split(" ")
a4 = "8 5 9 3".split(" ")
#print(a1[0])
sum = 0
y = 1
x = 0
#Read each of the above "a" variables 1 through 4
while y < 5:
if y == 1:
# sum = a1[0]
sum = locals()[f"a{y}"][x]
else:
# if a2[0] >= a2[1]
if locals()[f"a{y}"][x] >= locals()[f"a{y}"][x+1]:
sum = sum + locals()[f"a{y}"][x]
else:
sum = sum + locals()[f"a{y}"][x+1]
x+=1
y+=1
print(sum)
# output is 3749
sumint = 0
for i in range(0, len(sum)):
sumint = sumint + int(sum[i])
print(sumint)
# output is 23
# the last step of sumint could have been saved by using
# int(locals()[f"a{y}"][x])