My problem is following:
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"
Than I added following code:
else:
print "Word not found"
But this obviously can't work, because it will print "Word not found" until the word is found. Well.. but how can I print that the word is not found?! I srsly don't know.
Thank you in advance!
Python has a special trick for this kind of thing:
for line in f:
if word in line:
print "Word found"
break
else:
print "Word not found"
Here the else goes with the for, and specifically executes if the loop completes normally without hitting a break.
If all you want it to do is to print whether word is found in any of the lines:
def searchWordlist():
path = str(raw_input(PATH))
word = str(raw_input(WORD))
with open(path) as f:
if any(word in line for line in f):
print('Word found')
else:
print('Word not found')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With