在python语言中出现"TypeError: can only concatenate str (not "int") to str"
person = {
'first_name': 'willie',
'last_name': 'matthes',
'age': 8,
'city': 'sitka',
}
name = person['first_name'].title() + " " + person['last_name'].title()
age = person['age']
city = person['city'].title()
print("nName: " + name)
print("Age: " + age)
print("City: " + city)
输出后出现如下图错误:
根据错误提示可明白是错误是“str”与“int”不一致导致的,所以加减运算符号左右变量类型要一致,
有两种修改方法:
1.将“+”改为“,”
print("Age: ", age)
2.将“age"变量由“int”类型改为“str”类型
age = str(person['age'])



