简体   繁体   English

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

[英]How do I complete the 8.4.13 Owls, Part 2 problem in CodeHS Python?

So the assignment says:所以作业说:

In addition to just reporting how many words contained the word owl, you should report the indices at which the words occurred!除了只报告包含单词 owl 的单词数量之外,您还应该报告单词出现的索引 Here's an example run of your program might look like:以下是您的程序运行示例,可能如下所示:

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]

This is what I have so far for my code:到目前为止,这是我的代码:

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")

But that only prints the first index it's found at, how do I get it to print all of the indexes it's found at?但这仅打印它找到的第一个索引,我如何让它打印它找到的所有索引?

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()]

This works.这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM