Python Forum
Printing empty list? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Printing empty list? (/thread-30607.html)



Printing empty list? - hhydration - Oct-28-2020

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)


RE: Printing empty list? - deanhystad - Oct-28-2020

Don't double post


RE: Printing empty list? - Atekka - Oct-28-2020

If the way your code is indented is the way it is in your code snippet, you just have to indent the lines after your for loop as shown below. Hope that helps =]

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