【发布时间】:2018-03-29 19:49:10
【问题描述】:
我正在尝试编写从命令行接受文件名的代码,并且 打印出以下属性:
- 行数
- 字符数
- 字数
- “该”的数量
- “a/an”的数量
我不断收到错误消息
“'int'类型的参数不可迭代”
为if 'the' in words: 行。
我该如何解决这个问题?
import sys
import string
file_name=sys.argv[0]
char= words = lines = theCount = aCount= 0
with open(file_name,'r') as in_file:
for line in in_file:
lines +=1
words +=len(line.split())
char +=len(line)
if 'the' in words:
theCount +=1
if 'a' in words:
a +=1
if 'an' in words:
a +=1
print("Filename:", file_name)
print("Number of lines:", lines)
print("Number of characters:", char)
print("Number of 'the'", theCount)
print("Number of a/an:", aCount)
【问题讨论】:
-
请参阅官方文档中的 this
Counter配方,其中展示了如何做大部分您想做的事情。
标签: python python-3.x