I have a problem with else statement. How to make the code print not found? it keep print not found
import os
f = open(\'D:/Workspace/snacks.txt\', \"r\");
clas
First of all you need to close your file, you can use with wrapper to make python take care of it for you. Remove the class line. Rest of the code looks fine.
import os
with open('D:/Workspace/snacks.txt', "r") as f:
for line in f.readlines():
if line.find('chocolate') != -1 or line.find('milkshake') != -1:
print "found ", line
else:
print "not found"
with open('foo') as f:
lines = f.readlines()
for line in lines:
if 'chocolate' in line.lower():
print "Found: ", line
elif 'milkshake' in line.lower():
print "Found: ", line
else:
print "Not Found."