【发布时间】:2020-07-18 03:18:10
【问题描述】:
def check(file_name, string_to_search):
with open(file_name, 'r') as read_obj:
for line in read_obj:
if string_to_search in line:
return True
return False
while True:
word = input('Is the word positive? | ')
if check('positivewords.txt', word):
print('Word is positive')
elif check('negativewords.txt', word):
print('Word is negative')
else:
print('Word not in database')
该代码应该逐行读取 txt 文件并确定“word”变量是否完全等于这些行之一。问题是,无论何时运行,变量都不必完全相等。例如,假设其中一行是“免费”,我搜索“e”,它仍然会弹出它在 txt 文件中。提前致谢。
【问题讨论】: