If I want to get the value when the last loop completed,I can use last_a=a
But how to get the value when the first 2 loops are completed
Input:
A=[20,13,15,25,24,17,14,19,23,11]
B=[32,43,65,21,13,16,53,52,35,38]
last_a=last_b=0
last_last_a=0
last_last_b=0
for a,b in zip(A, B):
if a<last_a and b<last_b:
pass
last_a=a
last_b=b
print(last_last_a)
Expected Output
0
0
20
13
15
25
24
17
14
19
>Solution :
You need to add last_last_a = last_a before assigning to last_a. Also, you need to print before making those assignments, as these assignments serve for the next iteration of the loop. So:
for a,b in zip(A, B):
if a<last_a and b<last_b:
pass
print(last_last_a) # moved up
last_last_a = last_a # <-- added
last_a=a
last_b=b
As you don’t print last_last_b, there is no need for that name.