函数和if / elif语句总是先返回if语句

时间:2014-04-02 02:21:08

标签: python

好的,所以我正在尝试创建一个带有三个DNA碱基的程序,并为您提供免费的氨基酸。我可以有效地将DNA转录为互补RNA,但我的翻译功能不起作用。

Here is my code: 
def basematch(base):
if base == "A":
    return "U"
elif base == "T":
    return "A"
elif base == "G":
    return "C"
elif base == "C":
    return "G"
else:
    return "invalid"




rawdna = raw_input("Enter a raw DNA sequence: ")
comp = list(rawdna)
length = len(comp)

n = 0


codonlist = []


while n + 1 <= length:
    final = comp[n]
    finalbase = basematch(final)
    codonlist.append(finalbase)
    n = n + 1


rawRNA = "".join(codonlist)

def translate(codon):
    if codon == "GCU" or "GCC" or "GCA" or "GCG":
       print 'Ala'
    elif codon == "UUU" or "UUC":
       print "Phe"
    elif codon == "UUA" or "UUG":
       print "Leu" 
    elif codon == "UGU" or "UGC": 
       print "Cys"
    elif codon == "UGA" or "UAA" or "UAG": 
       print "Stop"
    elif codon == "UGG" or "UAU" or "UAC": 
       print "Tyr"
    elif codon == "UCU" or "UCC" or "UCA" or "UCG":
       print "Ser"
    elif codon == "CUU" or "CUC" or "CUA" or "CUG":
       print "Leu"
    elif codon == "CCU" or "CCC" or "CCA" or "CCG":
       print "Pro"
    elif codon == "CAU" or "CAC":
       print "His"
    elif codon == "CAA" or "CAG":
       print "Gln"
    elif codon == "CGU" or "CGC" or "CGA" or "CGG":
       print "Arg"

translate(rawRNA) 

每当我翻译rawRNA时,它总是返回'Ala'。

很抱歉长代码。谢谢你的帮助。

4 个答案:

答案 0 :(得分:9)

你误解了怎么样或者&#39;在这里工作。

if codon == "GCU" or "GCC" or "GCA" or "GCG":

装置

if (codon == "GCU") or "GCC" or "GCA" or "GCG":

它始终是真的,因为&#34; GCC&#34;是一个非空字符串。

你可能想要的是:

if codon in ["GCU", "GCC", "GCA", "GCG"]:

答案 1 :(得分:3)

该行没有正确检查。

if codon == "GCU" or "GCC" or "GCA" or "GCG":

or未延伸至==;它将检查if "GCC":,它总是True,因此它会在那里结束。

答案 2 :(得分:0)

您的答案是if条件的正确形式:

if codon in ["GCU", "GCC", "GCA", "GCG"]:

虽然你可以缩短一些的案例,特别是那个(假设输入序列当然有效):

if codon[:2] == "GC":

但我想指出的是,如果您使用的是Python 3.1或更高版本,那么很多可以更轻松地进行基本翻译:

>>> import string
>>> s = 'GATTACA'
>>> s.translate(s.maketrans('ATGC','UACG'))
'CUAAUGU'

执行(sequence to list, individual base translate appending to new list, new list to sequence with join)的任何真实单元格都会迅速选择反对它们: - )

答案 3 :(得分:0)

另一种方法是制作translatebasematch词典:

basematch = {'A' : 'U', 'T' : 'A', 'G' : 'C', 'C' : 'G'}

try:
    print basematch['A']
    print basematch['foo']
except KeyError:
    print 'invalid'

translate = {"GCU" : 'Ala', "GCC" : 'Ala', "GCA" : 'Ala', "GCG" : 'Ala',
             "UUU" : 'Phe', "UUC" : 'Phe', "UUA" : 'Leu', "UUG" : 'Leu',
             "UGU" : 'Cys', "UGC" : 'Cys',
             "UGA" : 'Stop', "UAA" : 'Stop', "UAG" : 'Stop',
             "UGG" : 'Tyr', "UAU" : 'Tyr', "UAC" : 'Tyr',
             "UCU" : 'Ser', "UCC" : 'Ser', "UCA" : 'Ser', "UCG" : 'Ser',
             "CUU" : 'Leu', "CUC" : 'Leu',"CUA" : 'Leu',"CUG" : 'Leu',
             "CCU" : 'Pro', "CCC" : 'Pro', "CCA" : 'Pro', "CCG" : 'Pro',
             "CAU" : 'His', "CAC" : 'His', "CAA" : 'Gln', "CAG" : 'Gln',
             "CGU" : 'Arg', "CGC" : 'Arg', "CGA" : 'Arg', "CGG" : 'Arg'}

print translate["UUU"]