I have run some tests and determined that nested loops do not perform what I’m looking to do.
Simply put, I am trying to iterate through two separate variables and perform a calculation using the elements of each simultaneously.
I am quite new to coding and haven’t read the entire python encyclopedia, so please forgive me if there is an obvious solution that I am missing.
This code is part of a program to determine if a 9 digit number is valid or not:
def calc(x, y):
z = []
if x*y < 9:
z.append(x*y)
else:
z.append(x+y)
print(z)
x = "130 692 544"
x1 = ''.join(x.split())
y = "121212121"
calc(int(x1[0]), int(y[0]))
This bit works correctly! Now I need to increase the index by one and repeat the process.
While I can probably work out a “x1[0] + 1” type of solution, it occurred to me that there may be a way to pull from both variables at the same time for the iteration.
for x1 and y in range(0, 10):
calc(int(x1), int(y))
# this seems like it is making the variable the iterator, rather than iterating through both variables
or
for i and j in x1 and y:
calc(int(i), int(j))
# this seems better but again, I'm not sure if I can run through both... and then run the result through the function.
So again, is there a way to iterate through multiple variables at once?