我的问题是:
def searchWordlist():
path = str(raw_input(PATH))
word = str(raw_input(WORD))
with open(path) as f:
for line in f:
if word in line:
print "Word found"
然后我添加了以下代码:
else:
print "Word not found"
但这显然是行不通的,因为在找到单词之前,它会打印“wordnotfound”。好。。但我怎么能打印出找不到这个词?!我真的不知道。你知道吗
提前谢谢!你知道吗
作为一个额外的好处,它可以跟踪单词在文件中的第一个出现位置(如果有的话)。你知道吗
如果您只想打印是否在任何行中找到
word
:Python有一个特殊的技巧:
这里的
else
与for
一起使用,如果循环正常完成而未命中break
,则具体执行。你知道吗相关问题 更多 >
编程相关推荐