Python TypeError: int() argument must be a string, a bytes原因

TypeError of int() function

During Python development, when using the int() function to convert or generate int type data, if Python throws and prompts TypeError: int() argument must be a string, a bytes-like object or a real number, not 39;complex', then the reason is that the parameter type passed to the int() function is wrong. As TypeError prompts, the parameters of the int() function must be string strings (numeric strings), similar byte objects, real number, etc., but not complex plural type data.

int() example code

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

Disclaimer: Content is for reference only!

Guess you like

Origin blog.csdn.net/weixin_47378963/article/details/134695321