【发布时间】:2020-11-05 12:54:35
【问题描述】:
我正在创建一个拼写检查程序,它会从字符串输入或文本文件中返回所有拼写错误的单词。我希望程序将拼写错误的单词数相加,并将其打印为统计数据。
我似乎不知道如何计算列表中的单词数,并将其添加到计数器中。
errorCount = 0
while True:
try:
selection = int(input("Would you like to enter a string (1), open a text file (2) or Quit (0) ?"))
if selection==1:
String = input("Please enter your string: ")
dictionaryList = readDictionaryFile(dictionaryFile)
stringList = readString(String)
errorList = findErrors(dictionaryList, stringList)
printErrors(errorList)
errorCount = errorCount + errorList.count()
【问题讨论】:
-
errorList是什么格式,是字符串列表还是字典
-
它是一个字符串列表,它的元素是字典列表中没有找到的单词
-
你可以调用 len(errorList) 这将返回列表中元素的数量
-
那我可以做errorCount = errorCount + len(errorList)吗?非常感谢
-
只是为了将来清楚,我把这个放在答案中
标签: python while-loop counter