-1

In the match_letters function, with 'place = string.index(letter)' i keep recieving the same error of 'Value Error: tuple.index(x): x not a tuple.' Does anyone know how to solve this? It will be much appreciated :) Thanks

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
George Rees
  • 33
  • 2
  • 9
  • **exact duplicate** of [Frequency analysis issues with tuple error](http://stackoverflow.com/questions/29825013/frequency-analysis-issues-with-tuple-error) – Artjom B. Apr 23 '15 at 15:25
  • You already received an answer to your **exact same** question. Don't re-post a question. You can leave comments under answers to work out potential issues. – Artjom B. Apr 23 '15 at 15:26

1 Answers1

1

descending_order returns a 3-tuple, namely return Letter_Count_Tuple, new_letter_Count, string, which you then assign to another local variable within freq_analysis called string. Either unpack the tuple you return, or index it to retrieve the string variable that is in descending_order's scope.

To get a visual, simply print string before indexing it within match_letters.

C.B.
  • 8,096
  • 5
  • 20
  • 34