Python Forum

Full Version: Why does this function print empty list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def kasinski(text):
    trigraphs=[]
    distances=[]
    for trigraph in range (len(text)-2):
        newtrigraph= text[trigraph:trigraph+3]
    if newtrigraph in trigraphs:
        distances.append(trigraph-(text.index(newtrigraph)))
    else:
        trigraphs.append(newtrigraph)
        trigraphs.append(trigraph)
        return distances
Can anyone identify why this function is returning "[]"? I am printing with the string "UZRZEGNJENVLISEXRHLYPYEGTESBJHJCSBPTGDYFXXBHEEIFTCCHVRKPNHWXPCTUQTGDJHTBIPRFEMJCNHVTCFSAIIJENREGSALHXHWZWRZXGTTVWGDHTEYXISAGQTCJPRSIAPTUMGZALHXHHSOHPWCZLBRZTCBRGHCDIQIKTOAAEFTOPYEGTENRAIALNRXLPCEPYKGPNGPRQPIAKWXDCBZXGPDNRWXEIFZXGJLVOXAJTUEMBLNLQHGPWVPEQPIAXATYENVYJEUEI"(this string does contain repeated trigraphs)
You get an empty list because you only test 1 trigraph; "UEI". I think you want to do your if/else statement inside the loop, but you do it after.

Once you fix that problem you have another to fix too. Where should that return be?