我已经写了这个函数,但我不断得到断言错误,我不知道为什么???在
def amino(codonfile, DNA):
"""This function reads the 64-line codon file and then converts the DNA string entered into an amino acid string."""
codonfile = []
for line in codonfile:
x,y = line.split()
if DNA == dict[x]:
return dict[y]
codonfile = open("codon.txt","r")
codonfile.close
assert(amino("codon.txt","TTT")== "Phe")
assert(amino("codon.txt","TTATTCTTGTCT")== "LeuPheLeuSer")
assert(amino("codon.txt","TTAGGGCCCTAC")== "LueGlyProTyr")
print("passed")
我假设您正在尝试解决your prior question中列出的相同问题。这次包括你的代码做得很好。在
以下是代码中的一些问题。在
^{pr2}$codonfile
是作为amino
的参数提供的,但您会立即用空列表覆盖它!由于codonfile
是一个空列表,因此没有什么可迭代的。你的for循环循环零次。相反,请执行以下操作:现在,for循环的内部也相当混乱。在
此时没有名为
dict
的字典。(但是dict
类型中有一个内置项,所以不要使用该名称)。相反,您需要用codonfile
中的项填充字典。在for循环之前,您需要创建一个字典来保存这些项。这样做:最后一位是使用
codon_table
来翻译DNA字符串。您需要将DNA
分成3个字母片段,并从codon_table
获得翻译。然后您需要将这些翻译连接到一个字符串中并返回它。在我把最后一段留作练习而没有执行。试一试,如果你拿不到,就把你试过的作为另一个问题。在
您的代码是专利废话,将始终返回
None
。在您忽略了打开的文件,将文件名传递给函数,然后用一个空列表替换那个,这样就结束了对空列表的循环,而不会产生任何结果。在
最后,每个
assert
语句将结果(None
)与字符串进行比较,字符串永远不会匹配,因此失败。在相关问题 更多 >
编程相关推荐