zoukankan      html  css  js  c++  java
  • 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:linuxpython	est2day1info.py", line 22, in <module>
        home:''' + home + '''
    TypeError: can only concatenate str (not "int") to str
    

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

    age = input("age:")
    
  • 相关阅读:
    dubbo注册zookeeper保错原因
    Django 终端打印SQL语句
    Django 的orm模型
    Django 的路由系统
    Django 开端
    前端 jq的ajax请求
    前端 后台
    前端 JQ操作
    前端 链式操作
    前端 JQ事件操作
  • 原文地址:https://www.cnblogs.com/wang50902/p/11660761.html
Copyright © 2011-2022 走看看