String int对象不可编辑?

String int对象不可编辑?,string,python-3.x,dictionary,String,Python 3.x,Dictionary,我试图创建一个程序,该程序将接受一个用户输入,这将是一个数字字符串,并打印出每个数字出现的次数。但是,我收到一个TypeError,指出int对象不可编辑?我该如何解决这个问题?为什么会发生?多谢各位 def main(): count = {} user_input = input("Enter numbers separated by spaces: ") for number in user_input.split(): if number in count: co

我试图创建一个程序,该程序将接受一个用户输入,这将是一个数字字符串,并打印出每个数字出现的次数。但是,我收到一个TypeError,指出int对象不可编辑?我该如何解决这个问题?为什么会发生?多谢各位

def main():
count = {}
user_input = input("Enter numbers separated by spaces: ")
for number in user_input.split():
    if number in count:
        count[number] = count[number] + 1
    else:
        count[number] = 1
print(count)

for k,v in count.values():
    if v == 1:
        print(k,"occurs one time")
    else:
        print(k,"occurs",v,"times")
main()

对于每个键,检查其值,如下所示:

for key in count:
    if count[key] == 1:
        print(key,"occurs one time")
    else:
        print(key,"occurs",count[key],"times")
count.values()
count.items()
将返回您的
对。

替换:

for k,v in count.values():
与:

对于循环,既需要键
k
,也需要值
v
count.values()
将只返回值<相反,code>count.items()
将同时返回这两个值

for k,v in count.items():