python产生错误:can only concatenate str (not "int") to str

代码为:

#!/usr/bin/python
# _*_ coding:utf-8_*_
# print("hello world!")
name = input("name:")
age = int(input("age:"))
print(type(age))
#print(type(age), type(str(age)))
home = input("home:")
print(type(home))
info3='''
---------info3 of ''' + name + ''' --------------
name: ''' + name + '''
age:''' + age + '''
home:''' + home + '''
--------end---------
'''
print(info3)

在使用python拼接进行格式化输出时出现如下错误:

Traceback (most recent call last):
  File "D:\linux\python\test2\day1\info.py", line 22, in <module>
    home:''' + home + '''
TypeError: can only concatenate str (not "int") to str

针对此问题,解决方法是:
主要是因为age的字段中使用int格式来验证,导致此问题。

age = input("age:")
posted @ 2019-10-12 12:05  wang509020  阅读(4159)  评论(0编辑  收藏  举报