我试图让我的程序从一个文本文件中抓取每五个单词并将其放在一个字符串中。例如,如果我输入“每个人都喜欢吃馅饼,因为它味道很好,而且它有很多品种,例如蓝莓草莓和酸橙”,那么程序应该打印出“每个人都因为加上品种和”。我必须从第一个单词开始,然后每五个单词抓一次。我对如何做到这一点感到困惑。下面是我的代码,除了最后 5 行之外,一切都运行良好。
#Prompt the user to enter a block of text.
done = False
textInput = ""
while(done == False):
nextInput= input()
if nextInput== "EOF":
break
else:
textInput += nextInput
#Prompt the user to select an option from the Text Analyzer Menu.
print("Welcome to the Text Analyzer Menu! Select an option by typing a number"
"\n1. shortest word"
"\n2. longest word"
"\n3. most common word"
"\n4. left-column secret message!"
"\n5. fifth-words secret message!"
"\n6. word count"
"\n7. quit")
#Set option to 0.
option = 0
#Use the 'while' to keep looping until the user types in Option 7.
while option !=7:
option = int(input())
#I'm confused here. This is where I'm stuck. Is the 'for' loop correct for this `#situation?`
#If the user selects Option 5,
elif option == 5:
for i in textInput.split():
if i <= 4 and i >= 6:
print(textInput)