Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the value when the first 2 loops are completed

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading