Monday, 11 November 2019

Specific number of input elements in a list

I have a list of words and my input is one of the words in the first half of the list (in this case, this list is a german song). Now I take the lenght of this word and jump this the length of this word in list, f.e. first word in the list is "Es" and the length is 2. Now we count from "Es" 2 times and we land by word "zwei". I should go through this list and check if the word appears again in the second half of the list.

The program works, but the problem is about the input. It takes one word and sees if the theorie works. This is the song list: "hin" is the last word of the first half

song = [
    "Es", "gingen", "zwei", "Parallelen",
    "ins", "Endlose", "hinaus",
    "zwei", "kerzengerade", "Seelen",
    "und", "aus", "solidem", "Haus", 

    "Sie", "wollten", "sich", "nicht", "schneiden", 
    "bis", "an", "ihr", "seliges", "Grab", 
    "Das", "war", "nun", "einmal", "der", "beiden", 
    "geheimer", "Stolz", "und", "Stab", 

    "Doch", "als", "sie", "zehn", "Lichtjahre", 
    "gewandert", "neben", "sich", "hin", #End of the first half of the song
    "da", "wards", "dem", "einsamen", "Paare", 
    "nicht", "irdisch", "mehr", "zu", "Sinn", 

    "Warn", "sie", "noch", "Parallelen",
    "Sie", "wusstens", "selber", "nicht", 
    "sie", "flossen", "nur", "wie", "zwei", "Seelen",
    "zusammen", "durch", "ewiges", "Licht", 

    "Das", "ewige", "Licht", "durchdrang", "sie",
    "da", "wurden", "sie" "eins", "in", "ihm", 
    "die", "Ewigkeit", "verschlang", "sie",
    "als", "wie", "zwei", "Seraphim"] 

I want my input to be all the words of the first half of the list (song in this case), not just one word. So it just prints out the result (result is a list in this case) for each word in every line.

This is how the output now looks

And I want it to immediatly print every result for each word in the first half and if the theorie works. This would be than the ouput:

Theorie works/doesn't

Result1

Theorie works/doesn't

Result2

Theorie works/doesn't

Result3

And so on...

Here is the code:

with open('C:/Users/xy/Desktop/BWINF/parallelen.txt', 'r') as f:

song = f.read()

noneed = "–?,.;:"
for char in noneed:
    song = song.replace(char, "")
song = song.split()

def Parallelen(listSong):

originalWord = input("Enter a word: ")
originalWordSaved = originalWord
theorie_list = [] # The list for found words
index = song.index(originalWord) # Get the index of the first instance of "word"
indexOriginal = song.index(originalWordSaved)
wordCount = song.count(originalWord)

while True:
    if indexOriginal > 42:
        print("Word is in the second half")
        break
    if wordCount <= 1:
        print("Word appears only 1 time and therefore can't appear one more time")
        print("Theorie doesn't work")
        break  
    try:
        theorie_list.append(listSong[index])
        theorie_list.append(len(listSong[index]))
        index += len(listSong[index]) 
        if listSong[index] == originalWordSaved:
            theorie_list.append(listSong[index])
            theorie_list.append(len(listSong[index]))
            print("Theorie works")
            break
    except:
        print("Theorie doesn't work") 
        break

return theorie_list

print(Parallelen(song))


from Specific number of input elements in a list

1 comment: