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:")


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM