Python Forum

Full Version: Loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am wondering if anyone can help with this question? I am pretty rubbish at coding, and it doesnt return anything so I'm not sure what is wrong, and not sure what method to use.

One of the tasks that bioinformaticians need to do is to detect protein-coding genes by looking for start and stop codons in the sequenced viral genomes. Write a Python function named 'start_present' in the following code cell that takes a single DNA sequence (string) as an argument and returns the Boolean value True if the sequence starts with 'ATG' and False if it does not.

def start_present(dna_seq1):
    #your code here
    raise NotImplementedError()

#my answer attempt 1

def start_present(dna_seq1):
if dna_seq1 == 'string':
        return True
    else:
        return False
raise NotImplementedError()

#my answer attempt 2

def start_present(dna_seq1):
if string in dna_seq1:
        return True
    else:
        return False
raise NotImplementedError()
Second attempt looks fine, except that you don't define string. Not sure why you are raising that exception.

Also, please post code using the python tags. That preserves formatting.
Try this.

def start_present(dna_seq1):
  if dna_seq1[:3] == "ATG":
    return True
  return False
What do you mean python tags jefsummers?

Thanks to both that helped me through it
(Nov-27-2020, 02:16 PM)MK_CodingSpace Wrote: [ -> ]Try this.

def start_present(dna_seq1):
  if dna_seq1[:3] == "ATG":
    return True
  return False

You might as well just write return dna_seq1[:3] == "ATG" as the body of that function; the rest is redundant.
There is string method startswith(), so you can just do:

def start_present(dna_seq1):
    return dna_seq1.startswith('ATG')
(Nov-27-2020, 06:09 PM)perfringo Wrote: [ -> ]There is string method startswith()

and it's pep8 recommended way to check for prefix, not slices
(not part of the assignment) - given the overhead associated with function calls a one line function is not very efficient. Better to just call the str.startswith() function inline.
if x==1: