print if else statement on python

后端 未结 2 1765
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 01:27

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         


        
2条回答
  •  耶瑟儿~
    2021-01-26 02:32

    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."
    

提交回复
热议问题