lundi 21 janvier 2019

Finding a string within an file

New to python and I can't figure out this code. I need to write a function called fileStringSearch() that accepts two arguments, one representing a file to open, and the second to accept a string. The function should return True if the string is present in the file. Otherwise, it should return False. The second argument must be at least 3 characters in length. If it is not, the function should return -1.

So far this is what I have, I think it's probably just an error with the if statement but thought I would let another coder look at it:

def filesStringSearch():
infile = open('example.txt')
a = input('Search for a word: ')
result = infile.read().find(a)
#result = a.find:
#for a in infile:
if a.find:
    print("True")
elif a < 3:
    print("-1")
else:
    print("False")
return

Any suggestions? Thanks!

Aucun commentaire:

Enregistrer un commentaire