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

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

相关推荐
码行山野赴时序归途5 分钟前
顺序表(Sequential List)详解:从数组到 C 语言实现
c语言·开发语言·数据结构·算法
扬大平仔8 分钟前
# 小深:用 AgentScope Java 2.0 Harness 做私人助手(上)mysql
java·开发语言·mysql
今天AI了吗8 分钟前
去中心化 AI 反馈系统:数据不上链,凭证与激励分开管
人工智能·windows·python·数据分析·去中心化·区块链·embedding
今朝唯我少年郎16 分钟前
Codex安全盲区代码漏洞生成实测
python·程序员
阿里云基础软件18 分钟前
一句话看透 JVM,SysOM 诊断 Skill 新增 Java 应用诊断能力
java·开发语言·jvm·人工智能·操作系统·sysom 诊断 skill
E_ICEBLUE19 分钟前
Python 实现 Excel 转 Markdown,支持工作表、单元格区域和批量处理
python·excel·markdown·格式转换
ctlover33 分钟前
Python高级与正则表达式
开发语言·python
泡海椒35 分钟前
生产环境安全配置:JQuickJavaInvocationGuard自定义防护规则实战
开发语言·python·安全
yume_sibai41 分钟前
07-Rust 异步编程完全指南(async/await + Tokio + Future + 并发原语 + 异步流)
开发语言·后端·rust