2015-04-23 58 views
0

在match_letters函數中,使用'place = string.index(letter)'我不斷收到'Value Error:tuple.index(x):x not a tuple'的錯誤。有誰知道如何解決這個問題?這將非常感激:)謝謝帶元組錯誤的頻率分析問題

def freq_analysis(): 
""" Summary: Perform a frequency analysis on a string 
    Paramaters: text - The text to analyse 
    Returns: A string containing the deciphered text 
""" 
    text = input("enter text") 
    letterCount = getLetterCount(text) 


    Letter_Count_Tuple,new_letter_count,descending = descending_order(letterCount) 
    ETAOIN = "ETAOINSHRDLCUMWFGYPBVKJXQZ" 
    string = descending_order(letterCount) 
    finish = match_letters(ETAOIN, string,text) 

def getLetterCount(text): 
    letterCount = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, 'Y': 0, 'Z': 0} 
    for letter in text.upper(): 
     if letter in letterCount: 
      letterCount[letter] += 1 

    print("Letter count is",letterCount) 
    return letterCount 

def descending_order(letterCount): 
    Letter_Count_Tuple = letterCount.items() 
    new_letter_Count = sorted(Letter_Count_Tuple, key = get_number) 
    print("new letter count is",new_letter_Count) 
    descending = new_letter_Count[::-1] 
    print("descending is",descending) 
    string = "" 
    for le in descending: 
     string = string + le[0] 

    print("String is",string) 
    return Letter_Count_Tuple, new_letter_Count, string 

def get_number(Letter_Count_Tuple): 
    return Letter_Count_Tuple[1] 

def match_letters(ETAOIN,string,text): 

# loop over each leter in the encrypted text 
    # find the position of that letter in your observed descending order string 
    # replace it with the letter in the same position from the etaoin descending order string 
    finish = "" 
    for letter in text: 
    place = string.index(letter) 
    ETAOIN_place = ETAOIN[place] 
    for le in ETAOIN_place: 
     finish = finish + le 
    print(finish) 

freq_analysis()

Traceback: 
Message File Name  Line Position  
Traceback        
    <module> <module1>  54    
    freq_analysis  <module1>  13    
    match_letters  <module1>  47    
ValueError: tuple.index(x): x not in tuple 
+0

你可以複製/粘貼完整的traceb ACK? –

+0

繼承人一個鏈接到pastebin,因爲它不粘貼得很好,謝謝:) http://pastebin.com/SCTnXube –

+0

只需將它添加到您的問題與正確的格式...這將使它更易於閱讀 –

回答

0

變量你傳遞給match_letters這裏:

string = descending_order(letterCount) 
finish = match_letters(ETAOIN, string,text) 

是不是真的一個字符串(或列表),它是一個元組,從降序

return Letter_Count_Tuple, new_letter_Count, string 

要麼返回剛剛在字符串descending_order或從返回的元組把它撿起來正確:

_,_,string = descending_order(letterCount) 

(我假設descending_order是你想要的那個在match_letters