Python初学者错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'
来源:互联网 发布:淘宝店铺首页大图 编辑:程序博客网 时间:2024/06/05 01:15
在大神们看来print可能是一个再简单不过的函数,但是对于新手来说,在自学过程中,会碰到各种各样的问题,如果没有很好的老师或者学习思路,很有可能学习成本会相当高,所以希望大神们不要鄙视初学者的“傻X”问题,毕竟大家都是过来人
如下代码:
>>> a = 1>>> print(a)1>>> b = 2>>> print(a + b)3>>> type(a)<type 'int'>
此时我们很想打印出如下效果:
>>> print(str(a) + '*' + str(b) + '=' + str(a * b))1*2=2
可是我们不知道为什么要加’str’,于是就出现如下错误:
>>> print(a + '*' + b + '=' + a * b)Traceback (most recent call last): File "<input>", line 1, in <module>TypeError: unsupported operand type(s) for +: 'int' and 'str'
其实错误提示已经很明确了,“类型错误:不支持操作类型为整数和字符串”,这里需要解释的最关键的东西是“+”,“+”在python中有两个作用,一个是数学运算符,是用来在整型、浮点型等数学之间进行加法操作的。另一个是用来进行字符串连接的。所以当你的“+”出现在即有数学运算和字符连接的情况下,计算机根本不知道哪两个是要进行字符串连接,哪两个之间要进行数学运算。
举一反三:
>>> print('a' + 2)Traceback (most recent call last): File "<input>", line 1, in <module>TypeError: cannot concatenate 'str' and 'int' objects>>> print(2 + "a")Traceback (most recent call last): File "<input>", line 1, in <module>TypeError: unsupported operand type(s) for +: 'int' and 'str'
0 0
- Python初学者错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'
- TypeError: unsupported operand type(s) for +: 'int' and 'str'
- Python PIP Install throws TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
- J版bug之_get_host_numa_topolo TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
- mysql unsupported operand type(s) for %: 'tuple' and 'tuple'
- python TypeError: bad operand type for unary -: 'unicode'
- Python---TypeError: '<' not supported between instances of 'str' and 'int'
- Type Incompatible operand types String and int
- Python:TypeError: unorderable types: int() < str()
- Python字符串与数字拼接 TypeError: cannot concatenate 'str' and 'int' objects
- Python 2.7 Data Type for Text Strings: Unicode and Str
- TypeError: '<' not supported between instances of 'str' and 'int'
- Python字符串与数字拼接报错解决TypeError: cannot concatenate 'str' and 'int' objects
- TypeError: unorderable types: str() > int()
- python错误:TypeError: 'str' object is not callable
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- 错误1error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::str
- Python3.6:“猜数字游戏”TypeError: '<' not supported between instances of 'str' and 'int'
- Android Studio的快捷键
- xutils请求shh框架发布的json数据(类似from表单)
- 使用Enumeration和Iterator遍历集合类
- android小知识——时间与时间戳之间的转化
- POJ3469
- Python初学者错误:TypeError: unsupported operand type(s) for +: 'int' and 'str'
- java文件操作笔记(File类的使用)
- activemq java版本要求
- mysql简单存储过程范例之遍历数据库生成视图
- android小知识——对图片的压缩方式
- spark解决报错WARN util.Utils: Service 'sparkWorker' could not bind on port 0. Attempting port 1.
- Linux下进程名字前面带[ ]的是什么进程
- Android小知识——横向scrollView是否滑到最右边
- C#学习笔记之线程