我想編寫的代碼,將在命令行和 打印出來接受一個文件名以下屬性時,「詮釋」不是可迭代:蟒蛇參數使用在
- 線 數
- 字符數
- 數量的單詞
- 數的「該」
- 數的「一/一個」
我不斷收到錯誤消息
「參數類型的 '詮釋' 不是可迭代的」
爲線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)來自官方文檔的'計數器'配方,它顯示瞭如何做你想要的大部分。 –