Last element of list not being taken into account for if statement

You exit the loop too early which is a common mistake:

for i in range(len(seq1)) :
    if seq1[i] == seq2[i]:
        print(" The sequences might be the same ")  # note "might"
        # exit()  # <- leave this one out
    else:
        print(" Sequences differ by a/mulitple nucleotide ")
        exit()
print(" The sequences are the same ")  # now you know

There is a built-in shortcut for this pattern (all) which you can combine with zip to make this more concise:

# ...
else:    
    if all(x == y for x, y in zip(seq1, seq2)):
        print(" The sequences are the same ")
    else:
        print(" Sequences differ by a/mulitple nucleotide ")

for lists, you can also just check equality:

if seq1 == seq2:
    print(" The sequences are the same ")
elif len(seq1) != len(seq2):
    print(" The sequences differ by their length ")
else:
    print(" Sequences differ by a/mulitple nucleotide ")

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top