lsykkk 发表于 2020-11-21 20:52:30

为什么报错

def count_words(filename):
    try:
      with open(filename)as a:
            contents=a.read()
    except FileNotFoundError:
      print("sorry,the file"+filename+"does not exist")
    else:
      words=contents.splt()
      number=len(words)
      print("the file"+filename+"has about"+ str(number) +"words")
filenames=['1.py','k.py']
for filename in filenames:
    count_words(filename)
   

Traceback (most recent call last):
File "/Users/lsy/Documents/pythonlsy/k.py", line 13, in <module>
    count_words(filename)
File "/Users/lsy/Documents/pythonlsy/k.py", line 8, in count_words
    words=contents.splt()
AttributeError: 'str' object has no attribute 'splt'

Twilight6 发表于 2020-11-21 21:32:22



函数名错了,是 split() 不是 splt() 哈

把 words=contents.splt() 改成 words=contents.split() 即可

页: [1]
查看完整版本: 为什么报错