def countWord(name, word):
try:
f = open(name, "r")
lst = ([])
w = ""
for x in f.readlines():
w += x.lower()
lst = w.split()
f.close()
return lst.count(word)
except FileNotFoundError:
print("Please create a file or use the name of an existing text file.")
print("Your word appears", countWord("gettysburg.txt", "random"), "time(s)")
The text file I used for testing looks like:
random words
random words
I'm putting random words in here
random
this is random
RaNdOm
I didn't really know what exceptions your professor is looking for so I just used the file not found one. Best of luck.