'Finding the length of RNA given certain conditions using Python

I'm tasked with designing a function which recognises the length of the RNA code (excluding the start and stop codons). The function must also determine if their code is actually valid (must contain AUG as a start codon and UGA, UAA, or UAG at the end) Note: RNA starts with a start codon AUG and ends with UGA, UAA, or UAG The function must return "Not readable RNA code" if the above conditions are not met.

def rna_length(mrna):
    start_trans = 'AUG'

    end_trans1 = 'UAA'
    end_trans2 = 'UGA'
    end_trans3 = 'UAG'   

    if ((mrna[0:3]!=start_trans) and (mrna [-3:]!=end_trans1 or end_trans2 or end_trans3)):

        return "Not readable RNA code"  

    else:
        (mrna[0:3]==start_trans) and (mrna [-3:]==end_trans1 or end_trans2 or end_trans3)

        length = len(mrna[3:-3]) 

        return length

But this code won't work for 'AUGAGGCACCUUCUGCUCCUUAC'. It returns the length instead of "Not readable'



Solution 1:[1]

The problem is in the condition :

if ((mrna[0:3]!=start_trans) and (mrna [-3:]!=end_trans1 or end_trans2 or end_trans3)):

You just need to change the and to an or, like this:

if ((mrna[0:3]!=start_trans) or (mrna [-3:]!=end_trans1 or end_trans2 or end_trans3)):

This is due to the fact that if the RNA code doesn't start with 'AUG', or if the code doesn't end in 'UAA', 'UGA' or 'UAG' - it is not a valid RNA code.

complete code:

def rna_length(mrna):
    start_trans = 'AUG'

    end_trans1 = 'UAA'
    end_trans2 = 'UGA'
    end_trans3 = 'UAG'   
    if ((mrna[0:3]!=start_trans) or (mrna [-3:]!=end_trans1 or end_trans2 or 
end_trans3)):

        return "Not readable RNA code"  

    else:
        (mrna[0:3]==start_trans) and (mrna [-3:]==end_trans1 or end_trans2 or 
end_trans3)

        length = len(mrna[3:-3]) 

        return length

 def main():
     print(rna_length("AUGAGGCACCUUCUGCUCCUUAC"))
 if __name__== "__main__":
     main()

output:

Not readable RNA code

Solution 2:[2]

I think you have a logic error in your Code:

In your IF-Check you want to check if:

The first three letters are 'AUG'

mrna[0:3] == start_trans

and if the three last letters are 'UAA' or 'UGA' or 'UAG':

mrna[-3:] == end_trans1 or end_trans2 or end_trans3

If both are true, the length should be returned. So if one is false we should get the error.

So your if check should be:

if (mrna[0:3] != start_trans) or (mrna[-3:] != end_trans1 or end_trans2 or end_trans3):

    return "Not readable RNA code"

for an even shorter IF-check I we write it to:

end_trans = ['UAA', 'UGA', 'UAG']

if not (mrna[0:3] == start_trans or mrna[-3:] in end_trans):
    return "Not readable RNA code"`

Solution 3:[3]

def rna_length(mrna):
    length_of_code = len(mrna)
    
    start_codon = mrna[:3]
    stop_codon = mrna[-3:]
    
    if (start_codon == 'AUG' and (stop_codon in ('UAG', 'UAA', 'UGA'))):
        return print(f"rna_length('{mrna}') == {length_of_code - 6}")
    else:
        return "Not readable RNA code"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 MunsMan
Solution 3 RiveN