我想编写的代码,将在命令行和 打印出来接受一个文件名以下属性时,“诠释”不是可迭代:蟒蛇参数使用在
- 线 数
- 字符数
- 数量的单词
- 数的“该”
- 数的“一/一个”
我不断收到错误消息
“参数类型的 '诠释' 不是可迭代的”
为线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)
参见[这](https://docs.python.org/3/library/ collections.html#counter-objects)来自官方文档的'计数器'配方,它显示了如何做你想要的大部分。 –