dimanche 2 mai 2021

python if statement not filtering out the cases

So I have a dictionary of character and their nickname.

namedict = {"Harry Potter": ["Harry","Harry Potter"]}

I would like to write a function to detect if the name appear in the sentence and if exist, what would be the emotional status of the character.

def emotion(character, text):
    with open(text,'r') as f:
        for name, nickname in namedict.items():
            if character in name:
                print(str(character)+"\n")
                for (i, line) in enumerate(f):  
                    if (alias in l for alias in nickname):
                        print("line "+str(i))
                        print(te.get_emotion(l))
            else:
                print("character not found")

while when I run the code with my text file, it just returns every line, and apparently it is not taking characters not exist in the name list from the loop.

text sample and result

Aucun commentaire:

Enregistrer un commentaire