目的是读一个文件,找出里面出现 the 的次数,为了把所有的大写也算进去,加入了 lower (),但是发现 lower ()放进去就报错,删掉就没问题。大家帮我看看怎么改好
def count_words(filename):
try:
with open(filename) as obj:
contents=obj.read()
except FileNotFoundError:
msg="sorry, the file "+filename+" does not exist."
print(msg)
else:
words=contents.split()
numbers=words.lower().count('the')
print("There are "+str(numbers)+" 'the' appears in "+ filename+".")
filename='alice.txt'
count_words(filename)
报的错误如下:
Traceback (most recent call last):
File "
exercise1010.py", line 14, in <module>
count_words(filename)
File "
exercise1010.py", line 10, in count_words
numbers=words.lower().count('the')
AttributeError: 'list' object has no attribute 'lower'
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/483792
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.