Python TypeError: int() argument must be a string, a bytes原因

int()函数的TypeError

Python开发过程中,使用int()函数来转换或生成int类型的数据时,如果Python抛出并提示TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex',那么原因在于传递给int()函数的参数类型有误,正如TypeError的提示,int()函数的参数必须是string字符串(数值字符串)、类似字节对象、real number数字等,而不可以是complex复数类型的数据。

int()实例代码

python 复制代码
>>> str1 = '123'
>>> type(str1)
<class 'str'>
>>> int(str1)
123
>>> byteobj = b'56'
>>> type(byteobj)
<class 'bytes'>
>>> int(byteobj)
56
>>> realnumber = 3.12
>>> type(realnumber)
<class 'float'>
>>> int(realnumber)
3
>>> complexnum = 1+3j
>>> type(complexnum)
<class 'complex'>
>>> int(complexnum)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'

原文:TypeError: int() argument must be a string, a bytes原因

免责声明:内容仅供参考!

相关推荐
小成202303202652 小时前
Linux高级02
linux·开发语言
知行合一。。。2 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y2 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
咸鱼2.02 小时前
【java入门到放弃】需要背诵
java·开发语言
ZK_H2 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐2 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子2 小时前
Java:异常(exception)
java·开发语言
lifewange2 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium273 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
cmpxr_3 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法