2021年5月2日星期日

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

https://stackoverflow.com/questions/67362419/python-if-statement-not-filtering-out-the-cases May 03, 2021 at 09:02AM

没有评论:

发表评论