2012-11-05 147 views
-1

我已經寫了這個功能,但我不斷收到斷言錯誤,我不知道爲什麼?聲明錯誤:?

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

顯然是因爲你的3個斷言之一失敗了...... –

+1

向我們展示回溯,以便我們可以幫助您進行調試。您可以添加一個斷言文本('assert(expression,「some assertion identify here text)),以幫助簡化調試。 –

+1

+1 2 Joran Beasley)顯示堆棧跟蹤。 – alexvassel

回答

5

您的代碼是專利廢話,並將始終返回None

您忽略了打開的文件,將文件名傳遞給您的函數,然後用替換列表,結果會循環顯示空列表並且不產生任何結果。

最後,您的每個assert語句將結果(None)與字符串進行比較,字符串永遠不會匹配並因此失敗。

+0

和'codonfile.close'並不真正幫助 – lolopop

4

我假設您正在嘗試解決與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循環循環零次。而是這樣做:

def amino(codonfile, DNA): 
    with open(codonfile, 'r') as f: # opens the file and assigns to f 
     for line in f: 

現在,你的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獲得翻譯。然後,您需要將這些翻譯合併爲一個字符串並將其返回。

我沒有將這個最後一段作爲練習。給它一個鏡頭,如果你不能得到它發佈你已經嘗試過的另一個問題。

+1

+1,*這*是回答(可能的)作業問題的方式! – Charles