统计一段话中字母出现的次数
message=‘there is no more thing; should be quit‘
count={}
	for character in message:
	count[character]=count[character]+1
print(count)报错如下
Traceback (most recent call last):
  File "charactercount.py", line 4, in <module>
    count[character]=count[character]+1
KeyError: ‘t‘key值无效
message=‘there is no more thing; should be quit‘
count={}
	for character in message:
	count.setdefault(character,0)
	count[character]=count[character]+1
	print(count)打印结果如下
{‘t‘: 1}
{‘t‘: 1, ‘h‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 1, ‘r‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 1, ‘i‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 1, ‘i‘: 1, ‘s‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 2, ‘i‘: 1, ‘s‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 2, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 2, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 3, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 3, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 1, ‘m‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 1, ‘ ‘: 3, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 2, ‘r‘: 2, ‘ ‘: 3, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 3, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 1, ‘h‘: 1, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 2, ‘h‘: 1, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 2, ‘h‘: 2, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 1, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 2, ‘h‘: 2, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 1, ‘n‘: 1, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 2, ‘h‘: 2, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 1, ‘n‘: 2, ‘o‘: 2, ‘m‘: 1}
{‘t‘: 2, ‘h‘: 2, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 1, ‘n‘: 2, ‘o‘: 2, ‘m‘: 1, ‘g‘: 1}
{‘t‘: 2, ‘h‘: 2, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 1, ‘n‘: 2, ‘o‘: 2, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1}
{‘t‘: 2, ‘h‘: 2, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 2, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 2, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 4, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1, ‘d‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 5, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1, ‘d‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 3, ‘r‘: 2, ‘ ‘: 5, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 4, ‘r‘: 2, ‘ ‘: 5, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 4, ‘r‘: 2, ‘ ‘: 6, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 4, ‘r‘: 2, ‘ ‘: 6, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 1, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1, ‘q‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 4, ‘r‘: 2, ‘ ‘: 6, ‘i‘: 2, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 2, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1, ‘q‘: 1}
{‘t‘: 2, ‘h‘: 3, ‘e‘: 4, ‘r‘: 2, ‘ ‘: 6, ‘i‘: 3, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 2, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1, ‘q‘: 1}
{‘t‘: 3, ‘h‘: 3, ‘e‘: 4, ‘r‘: 2, ‘ ‘: 6, ‘i‘: 3, ‘s‘: 2, ‘n‘: 2, ‘o‘: 3, ‘m‘: 1, ‘g‘: 1, ‘;‘: 1, ‘u‘: 2, ‘l‘: 1, ‘d‘: 1, ‘b‘: 1, ‘q‘: 1}每次内循环,就会打印一次
message=‘there is no more thing; should be quit‘
count={}
count.setdefault(character,0)
	for character in message:
	count[character]=count[character]+1
	print(count)报错如下
Traceback (most recent call last):
  File "charactercount.py", line 3, in <module>
    count.setdefault(character,0)
NameError: name ‘character‘ is not defined未定义变量
原文:https://www.cnblogs.com/yuvejxke/p/11918433.html