断言错误:?

2024-10-02 18:16:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写了这个函数,但我不断得到断言错误,我不知道为什么???在

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

Tags: the函数txtstringdef错误lineassert
2条回答

我假设您正在尝试解决your prior question中列出的相同问题。这次包括你的代码做得很好。在

以下是代码中的一些问题。在

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:

codonfile是作为amino的参数提供的,但您会立即用空列表覆盖它!由于codonfile是一个空列表,因此没有什么可迭代的。你的for循环循环零次。相反,请执行以下操作:

^{pr2}$

现在,for循环的内部也相当混乱。在

    for line in codonfile:
        x,y = line.split()
        if DNA == dict[x]:
            return dict[y]

此时没有名为dict的字典。(但是dict类型中有一个内置项,所以不要使用该名称)。相反,您需要用codonfile中的项填充字典。在for循环之前,您需要创建一个字典来保存这些项。这样做:

codon_table = {} # brackets for an empty dictionary
for line in f:
    x, y = line.split()
    codon_table[x] = y  

最后一位是使用codon_table来翻译DNA字符串。您需要将DNA分成3个字母片段,并从codon_table获得翻译。然后您需要将这些翻译连接到一个字符串中并返回它。在

我把最后一段留作练习而没有执行。试一试,如果你拿不到,就把你试过的作为另一个问题。在

您的代码是专利废话,将始终返回None。在

您忽略了打开的文件,将文件名传递给函数,然后用一个空列表替换那个,这样就结束了对空列表的循环,而不会产生任何结果。在

最后,每个assert语句将结果(None)与字符串进行比较,字符串永远不会匹配,因此失败。在

相关问题 更多 >