如何在CodeHS Python中完成8.4.13 Owls第2部分的问题?

2024-05-18 20:14:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以作业上说:

除了报告包含单词owl的单词数量外,还应该报告单词出现的索引! 下面是您的程序运行的一个示例:

Enter some text: Owls are so cool! I think snowy owls might be my faborite. Or maybe spotted owls.
There were 3 words that contained "owl".
They occurred at indices: [0, 7, 15]

到目前为止,我的代码是这样的:

sentence = input("Talk about something: ")
sentence.lower()

print "There are " + str(sentence.count("owls)) + " of the word \"owls\" in the sentence"
print "They occurred at indices: " + str(sentence.findl("owls")

但是它只打印它在的第一个索引,如何让它打印它在的所有索引


Tags: the报告作业单词owlaresentenceat
1条回答
网友
1楼 · 发布于 2024-05-18 20:14:56
sentence = 'Owls are so cool! I think snowy owls might be my faborite. Or maybe spotted owls.'
words = [i for i, word in enumerate(sentence.split(' ')) if 'owl' in word.lower()]

这很有效

相关问题 更多 >

    热门问题