我有這個函式,用于在文本檔案中查找特定字串:
def check_for_string(string, file):
with open(file) as target_file:
for index, line in enumerate(target_file):
if string in line:
return True
break
當它找到特定的字串并回傳 True 時它會停止。我該怎么做,以便在找到字串時回傳 True,如果未找到字串則回傳 False?
uj5u.com熱心網友回復:
只需回傳False:
def check_for_string(string, file):
with open(file) as target_file:
for index, line in enumerate(target_file):
if string in line:
return True
return False
注意:您不需要投入break后return,也沒用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362615.html
