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

Viewed 64

I'm an absolute beginner at python and I need to write a code which can differentiate between 2 lists. Overall code works yet, consistently the last element of the list is not being taken into account and lists such as "AT, AC" are being considered the same. I would love some help. Thanks !

Seq1 = input( " Enter first sequence ")
Seq2 = input(" Enter second sequence ")

seq1 = list(Seq1)
seq2 = list(Seq2)

def compare_seq(seq1,seq2):
 if len(seq1) != len(seq2):
  print(" The sequences differ by their length ")
  exit()
 else:
  for i in range(len(seq1)) :
   if seq1[i] == seq2[i]:
    print(" The sequences are the same ")
    exit()
   else :
    print(" Sequences differ by a/mulitple nucleotide ")
    exit()

compare_seq(seq1,seq2)

4 Answers

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 ")

you are only checking for the first element and exiting.

Seq1 = input( " Enter first sequence ")
Seq2 = input(" Enter second sequence ")

seq1 = list(Seq1)
seq2 = list(Seq2)

flag = False

def compare_seq(seq1,seq2):
 if len(seq1) != len(seq2):
  print(" The sequences differ by their length ")
  exit()
 else:
  for i in range(len(seq1)) :
   if seq1[i] == seq2[i]:
    continue
   else :
    flag = True
    break
 
 if flag == False:
  print(" The sequences are the same ")
 else:
  print(" Sequences differ by a/mulitple nucleotide ")

 exit()

compare_seq(seq1,seq2)

the above code should help you. It check for the whole list instead of just and changes the flag to True if the elements don't match

Nearly had it but a few issues:

Seq1 = input( " Enter first sequence ")
Seq2 = input(" Enter second sequence ")

seq1 = list(Seq1)
seq2 = list(Seq2)

def compare_seq(seq1,seq2):
 if len(seq1) != len(seq2):
  print(" The sequences differ by their length ")
  #exit()  No need for this exit as it quits python and makes you have to reopen it everytime you run your function
 else:
   if seq1 == seq2:  #the original loop structure was comparing everysingle item in the list individually, but was then exiting python before completion. So in reality only the first element of each sequence was being compared
    print(" The sequences are the same ")
   else :
    print(" Sequences differ by a/mulitple nucleotide ")

compare_seq(seq1,seq2)

This should do the trick.

You can just check if any of the values at an index of the two lists are not equal, otherwise return true.

Example:

def compare_seq(seq1,seq2):
 if len(seq1) != len(seq2):
     print("sequences dont share the same length")
     return false

 for i in range(len(seq1)):
     if seq1[i] != seq2[i]:
         print("sequences are not the same")
         return false
 
 return true

Related