Python Forum
Why does this function print empty list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does this function print empty list?
#1
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)
Reply
#2
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?
hhydration likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can anyone spot why this function is returning an empty list? hhydration 2 1,868 Nov-18-2020, 06:16 AM
Last Post: deanhystad
  How to print all iterations of min and max for a 2D list alaina 4 2,925 Nov-11-2020, 05:53 AM
Last Post: alaina
  List of Objects print <__main. Problem Kol789 10 3,554 Jul-21-2020, 09:37 AM
Last Post: DeaD_EyE
  Question on "define function"; difference between return and print extricate 10 4,758 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  How can I print the number of unique elements in a list? AnOddGirl 5 3,311 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,573 Feb-27-2020, 09:22 AM
Last Post: buran
  why is this not filling my empty list? Siylo 4 3,186 Jan-21-2019, 05:27 PM
Last Post: Siylo
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,233 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,787 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  why is the append function gets empty on the loop?/python rhai 3 3,251 Jul-07-2018, 12:19 PM
Last Post: rhai

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020