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原因

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

相关推荐
文人sec34 分钟前
pytest1-接口自动化测试场景
软件测试·python·单元测试·pytest
Run_Teenage36 分钟前
C++:智能指针的使用及其原理
开发语言·c++·算法
码界奇点2 小时前
Java设计模式精讲从基础到实战的常见模式解析
java·开发语言·设计模式·java-ee·软件工程
四维碎片2 小时前
【Qt】配置安卓开发环境
android·开发语言·qt
西游音月2 小时前
(7)框架搭建:Qt实战项目之主窗体导航栏、状态栏
开发语言·qt
secondyoung2 小时前
Mermaid流程图高效转换为图片方案
c语言·人工智能·windows·vscode·python·docker·流程图
3***49962 小时前
Swift Experience
开发语言·ios·swift
iFlow_AI2 小时前
iFlow CLI Hooks 「从入门到实战」应用指南
开发语言·前端·javascript·人工智能·ai·iflow·iflow cli
nini_boom3 小时前
**论文初稿撰写工具2025推荐,高效写作与智能辅助全解析*
大数据·python·信息可视化
Maybyy3 小时前
Chart.js图标绘制工具库
开发语言·javascript·ecmascript