#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Question About Excluding An Empty Space As A Valid Entry


    Hello,

    So I made a function in Python that checks a DNA sequence to see whether or not the DNA base-pair sequence that the user enters is valid.

    By the way, as a side note, base pairs are basically what is listed when people state the "Genetic code" or the components of a single DNA strand (even though there are more individual parts than that, it makes it simpler to work out genomic patterns and the like)

    Anyway, valid bases in DNA include Adenine (A), Guanine (G), Thymidine (T), and Cytosine (C).

    What I want to implement in the following code is to state a boolean expression that reads false when a user enters nothing aka ''

    What I'm having trouble with is the fact that it comes up as True in the Python interpreter regardless.

    Here's my code:

    Code:
    def is_valid_sequences(dna_seq):
    
        '''(str) -> bool
    
        Return True if and only if the DNA sequence is valid
        (it contains no characters other than 'A', 'T', 'G', and 'C')
    
        >>> is_valid_sequences('ATGCATGC')
        True
    
        >>> is_valid_sequences('ATATATA')
        True
    
        >>> is_valid_sequences('KLMNOPQRS')
        False
    
        >>> is_valid_sequences('ZZYZZYZ')
        False
    
        First approach: Count the number of characters that are not nucleotides
        and then at the end, check whether there were more than zero.
    
        Second Approach: Use a Boolean variable that represents whether you have
        found a non-nucleotide character, it would start of as True and would be
        set to False if you found something that wasn't A,T, G, or C
    
        '''
    
        invalid = 0
    
        for char in dna_seq:
    
        # This will check each character in the DNA sequence
    
            if char not in 'ATCGatcg':
    
        # This is an if loop that will activate if ATCG is not found within the
        # string
                invalid = invalid + 1
    
            if char in '':
                print('FALSE')
    
    
    
        # This will increment each time the program comes across a invalid DNA
        # base
    
        return (invalid == 0)
    
        # This will occur if the amount of invalid DNA bases are equal to zero
        # which in turn would be TRUE for the overall argument of whether or not
        # the DNA sequence entered was valid or not
    
    # Status: Complete

    Any ideas on where I'm wrong? Thanks so much for reading my post and have an awesome day!

    By the way, I only included the Print False statement so that I could test to see whether or not that portion of the program was working, just as a safety measure.
    Last edited by HauntJemimah; September 15th, 2013 at 03:41 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    Everything under this line will not be executed if an empty string is submitted
    Code:
    for char in dna_seq:
    Try instead something like
    Code:
    if len(dna_seq.strip()):  ## removes white space
        for char in dna_seq:
            .....
    else:
        print('FALSE')
    Last edited by dwblas; September 15th, 2013 at 04:27 PM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Code:
    def is_valid_sequences(dna_seq):
        return not not (dna_seq and set(dna_seq.upper()).issubset('ACTG'))

    Comments on this post

    • codergeek42 agrees : Clever use of set operations...I like it.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Thanks a bunch guys! Awesome!

IMN logo majestic logo threadwatch logo seochat tools logo