The function should return the rest of the words from the file, just not the nth words.
I’ve got the opening, reading and closing the text file down, but I’ve been struggling to figure out the rest. I tried using append but quickly realized that’s not the way to go about it.
My failed coding so far:
def message(fname, n):
infile = open(fname,'r')
mess = infile.read()
infile.close()
mess.append('')
return mess
So it should return the rest of the words from the file, just not the nth words.
One could read the entire file into a list and use the del statement to remove every n-th item.
f.read() function reads the entire file as a string; split() function splits this string by whitespace; words[n – 1::n] is a list slice that says, starting at (n – 1)-th position, include every n-th item; del statement removes this slice from the list.