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

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

相关推荐
脾气有点小暴4 分钟前
前端页面跳转的核心区别与实战指南
开发语言·前端·javascript
rit843249928 分钟前
基于MATLAB的BP神经网络手写数字识别
开发语言·神经网络·matlab
独行soc35 分钟前
2025年渗透测试面试题总结-275(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
San30.1 小时前
深入 JavaScript 内存机制:从栈与堆到闭包的底层原理
开发语言·javascript·udp
灰灰勇闯IT1 小时前
RN路由与状态管理:打造多页面应用
开发语言·学习·rn路由状态
wd_cloud1 小时前
QT/6.7.2/Creator编译Windows64 MySQL驱动
开发语言·qt·mysql
亭上秋和景清1 小时前
指针进阶:函数指针详解
开发语言·c++·算法
胡萝卜3.01 小时前
C++现代模板编程核心技术精解:从类型分类、引用折叠、完美转发的内在原理,到可变模板参数的基本语法、包扩展机制及emplace接口的底层实现
开发语言·c++·人工智能·机器学习·完美转发·引用折叠·可变模板参数
9ilk1 小时前
【C++】--- C++11
开发语言·c++·笔记·后端
biter down2 小时前
C++ 函数重载:从概念到编译原理
开发语言·c++