'Why does this throw an infinite loop

def word_chain(a,b):
    with open(a) as file:
        words = {}
        text = file.read().split("\n")
        for t in text:
            words[t] = t[len(t)-2::]
        word = b
        while word[len(word)-2::] != "gs" and word[len(word)-2::]!= "ss" and word[len(word)-2::] != "ns":
            for z in words:
                if words[z] == word[(len(word))-2::]:
                    print (z)
                    word = z
                else:
                    pass
                break
                    
        print(b)

This program should take a list of words thats last two letter are the first two letters of another word in the document a. The variable b is the starting word. My while loop and then for loop throw an infinite loop and I am not sure why. There are no words in the document that end is gs, ss, ns which is why the while loop makes sense to me.

enter image description here



Solution 1:[1]

YOU CAN DO

READ LINES BY

text = file.readlines()

Main reason is because..

You are using AND Gate in between while loops..

like

How can 5th letter of ironman be "g" and "m" same time..

Try using OR

In While Loop

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EL BEAST Z