Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops
#1
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()
buran write Nov-27-2020, 04:12 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
Reply
#3
Try this.

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

Thanks to both that helped me through it
Reply
#5
(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.
buran likes this post
Reply
#6
There is string method startswith(), so you can just do:

def start_present(dna_seq1):
    return dna_seq1.startswith('ATG')
buran likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(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.
Reply
#9
if x==1:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020