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

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

相关推荐
ZHSH.1 分钟前
2026蓝桥杯备赛 | 赛事介绍及python基础(未完)
python·蓝桥杯·数据结构与算法
长安牧笛1 分钟前
设计残疾人出行路线规划工具,输入起点终点,自动筛选无障碍通道,电梯,盲道路线,避开台阶和陡坡。
python
一勺-_-19 分钟前
mermaid图片如何保存成svg格式
开发语言·javascript·ecmascript
Dargon2881 小时前
实例讲解Simulink的MATLAB Function模块
开发语言·matlab·simulink·mbd软件开发
charlie1145141911 小时前
现代C++嵌入式教程:C++98基础特性:从C到C++的演进(1)
c语言·开发语言·c++·笔记·学习·教程
历程里程碑1 小时前
C++ 18智能指针:告别内存泄漏的利器
开发语言·c++
程序员佳佳1 小时前
2025年大模型终极横评:GPT-5.2、Banana Pro与DeepSeek V3.2实战硬核比拼(附统一接入方案)
服务器·数据库·人工智能·python·gpt·api
刘某的Cloud2 小时前
列表、元组、字典、集合-组合数据类型
linux·开发语言·python
梁同学与Android2 小时前
Android ---【经验篇】ArrayList vs CopyOnWriteArrayList 核心区别,怎么选择?
android·java·开发语言