代碼為:
#!/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:")