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

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

相关推荐
CHANG_THE_WORLD28 分钟前
Python 学习三 Python字符串拼接详解
开发语言·python·学习
诸葛老刘32 分钟前
next.js 框架中的约定的特殊参数名称
开发语言·javascript·ecmascript
测试老哥44 分钟前
Postman接口测试基本操作
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
霸王大陆1 小时前
《零基础学 PHP:从入门到实战》模块十:从应用到精通——掌握PHP进阶技术与现代化开发实战-2
android·开发语言·php
winfredzhang1 小时前
基于wxPython的TodoList任务管理器开发详解
python·wxpython·todolist·持久
釉色清风1 小时前
在openEuler玩转Python
linux·开发语言·python
han_hanker1 小时前
这里使用 extends HashMap<String, Object> 和 类本身定义变量的优缺点
java·开发语言
Blossom.1181 小时前
基于多智能体强化学习的云资源调度系统:如何用MARL把ECS成本打下来60%
人工智能·python·学习·决策树·机器学习·stable diffusion·音视频
@小码农1 小时前
2025年北京海淀区中小学生信息学竞赛第二赛段C++真题
开发语言·数据结构·c++·算法
sulikey1 小时前
C++模板初阶详解:从函数模板到类模板的全面解析
开发语言·c++·模板·函数模板·类模板