这就是我该做的
这个程序是你以前的“猫头鹰”计划的扩展。你可以在这里找到你以前的程序! 除了报告包含单词owl的单词数量外,您还应该报告单词发生的索引! 下面是运行程序的示例可能是什么样子: 输入一些文字:猫头鹰太酷了!我想下雪的猫头鹰可能是我的最爱。也可能是斑点猫头鹰。 有三个词包含“猫头鹰”。它们发生在indices:
[0, 7, 15]中,正如您从输出中看到的那样,您必须使用另一个列表来存储包含“owl”的单词的索引。 枚举函数也会派上用场!
这是我现在的代码:
text = input("Enter some text: ")
text.lower()
text.split()
print "There are " + str(text.count("owl")) + " words that contained \"owl\"."
print "They occured at indices: for c, value in enumerate(text, 1):
print(c, value)我完全不知道如何使用enumerate函数。我现在使用enumerate函数的方式是从其他网站得到的。当我试图运行这段代码时,首先得到的是这个错误:
Error: Line 5
ParseError: bad token on line 5由此,我知道我使用enumerate的方式是错误的。然而,我不知道如何使用它。
发布于 2019-05-16 16:18:08
在这一行中:
print "They occured at indices: for c, value in enumerate(text, 1):
print(c, value)您还没有用"结束字符串,因此Python继续阅读越来越多,假设这个字符串还有更多的字符串,直到它到达文件的末尾为止。您不需要这样做,因为for c, value in enumerate(text, 1):是您希望Python执行的命令,它本身并不是您希望Python打印的字符串。因此,首先我们关闭您的字符串:
print "They occured at indices: "
for c, value in enumerate(text, 1):
print(c, value)这应该可以消除您的错误,但会打印错误的答案。这一行还有另一个问题:text.split(),您不了解split()是如何工作的。那部分的研究就交给你了。
发布于 2021-12-31 01:18:40
text = input("Enter some text: ")
text.lower()
text.split()
print "There are " + str(text.count("owl")) + " words that contained \"owl\"."
print "They occured at indices: for c, value in enumerate(text, 1):"
print "They occured at indices: "
for c, value in enumerate(text, 1):
print(c, value)https://stackoverflow.com/questions/56172334
复制相似问题