I get a syntax error and an error saying illegal target for variable annotation. How do I fix it?

Do you mean to write x += 1 statement inside if block and elif block like below:

def comparetriplets(a, b):
    p = 0                       
    q = 0                       
    x = 0                           
    while x < 3:
        if a[x] > b[x]:
            print('Begining of if :' , x)
            p = 1               
            q = 0               
            x += 1
            print('End of if :', x)
        elif b[x] > a[x]:
            print('Begining of elif-1 :' , x)
            p = p+0             
            q = q+1             
            x += 1
            print('End of elif-1 : ', x)
        elif a[x] == b[x]:    
            print('Begining of elif-2 : ' , x)
            p = p+0             
            q = q+0             
            x += 1
            print('End of elif-2 : ', x)
     return [p, q]               

Please let me know if it clarifies your doubt or solves the syntax error.

Calling the function comparetriplets:

import numpy
import random
a = random.sample(range(1,51),4) # Generate a list of four random numbers
b = random.sample(range(1,51),4)

print(a)
print(b)
p,q = comparetriplets(a, b)

Output:

a : [32, 3, 27, 21]
b : [20, 8, 35, 37]
Begining of if : 0
End of if : 1
Begining of elif-1 : 1
End of elif-1 :  2
Begining of elif-1 : 2
End of elif-1 :  3

Leave a Comment