Untitled

 avatar
unknown
python
3 years ago
1.4 kB
8
Indexable
import random 

class DNA:

  def __init__(self, n):
    '''intialzies a DNA object with length n and a randomly generated strand'''

    self.length = n
    self.strand = ""
    for i in range(n):
      self.strand += random.choice(['A', 'T', 'C', 'G'])


  def __str__(self):

    return self.strand + "of length " + self.n

  def is_complement(self, other):
    complement_dict = {
      'A': 'T',
      'T': 'A', 
      'C': 'G',
      'G': 'C'        
      }

    if self.length != other.length:
      return False
    
    for base_1, base_2 in zip(self.strand, other.strand):
      if complement_dict[base_1] != base_2:
        return False
    return True      


def is_reverse(self, other):
  return self.strand == other.strand[::-1]

def main():

    DNA_objects = []

    while True: 
      try:
        
        n = int(input("Length of DNA strand? "))
        r = int(input("Number of strands to test? "))
        
        if n <= 0 or r <= 0:    
          raise ValueError("\nNumbers must be positive. Please Try again")

        break

      except ValueError as e:
        print(f"\n{e}")

      except Exeption as e:
        print("\nInvalid Input, please try again.")


    # use "_" as variable since it's not being used
    for _ in range(r):
      dna = DNA(n)
      print(f'Creating strand: {dna}')
      DNA_objects.append(dna)

if __name__ == "__main__":
    main()
    
Editor is loading...