【发布时间】:2017-06-29 05:35:17
【问题描述】:
我想在我的程序中做的是让程序打开一个包含许多不同单词的文件。
接收用户输入并检查文件中的任何单词是否在用户输入中。
文件内redflags.txt:
happy
angry
ball
jump
每个单词在不同的行。
例如,如果用户输入是"Hey I am a ball",那么它将打印红旗。
如果用户输入是"Hey this is a sphere",那么它将打印 noflag。
Redflags = open("redflags.txt")
data = Redflags.read()
text_post = raw_input("Enter text you wish to analyse")
words = text_post.split() and text_post.lower()
if data in words:
print("redflag")
else:
print("noflag")
【问题讨论】:
-
对于初学者来说,这个:
words = text_post.split() and text_post.lower()并没有做你认为它正在做的事情。你想要words = text_post.lower().split()。