I'm supposed to use a txt file and select every 3rd word and assign it to the variable "three". This is what i have so far I don't know where to go from here.
fname = "school_prompt.txt"
with open(fname, 'r') as f:
for line in f:
three = f.read().split()
for line in f:
already puts the line as a string in line
. You shouldn't do f.read()
afterwards.
You want the whole text first so do this:
fname = "school_prompt.txt"
with open(fname, 'r') as f:
text = f.read()
Now text
is the content of the whole file. You can split it: words = text.split()
Now it should be easy to get every third word in a list:
three = words[0::3]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.