1、错误描述
>>> import time;
>>> di={1:'A',2:'B'};
>>> for key,value in dict.items(di):
print(key+","+value);
time.sleep(2);
Traceback (most recent call last):
File "<pyshell#5>", line 2, in <module>
print(key+","+value);
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
2、错误原因
在打印key和value时,key是int类型,value是str类型,直接利用逗号将它们连接起来会报错
3、解决办法
将key转换成字符串,然后再跟value连接起来打印
print(str(key)+","+value);