Python Forum
Loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Loops (/thread-31194.html)



Loops - Tink - Nov-27-2020

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



RE: Loops - jefsummers - Nov-27-2020

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.


RE: Loops - MK_CodingSpace - Nov-27-2020

Try this.

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



RE: Loops - Tink - Nov-27-2020

What do you mean python tags jefsummers?

Thanks to both that helped me through it


RE: Loops - ndc85430 - Nov-27-2020

(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.


RE: Loops - perfringo - Nov-27-2020

There is string method startswith(), so you can just do:

def start_present(dna_seq1):
    return dna_seq1.startswith('ATG')



RE: Loops - buran - Nov-28-2020

(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


RE: Loops - jefsummers - Nov-29-2020

(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.


RE: Loops - GirishaSJ - Dec-03-2020

if x==1: