为什么 Python 会为 int() 的有效字符串抛出 ValueError?
Why is Python throwing a ValueError for what appears to be a valid string for int()?
为什么会这样?:
>>> int('20', 3)
6
>>> int('8', 3)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
int('8', 3)
ValueError: invalid literal for int() with base 3: '8'
是的,我看过这个post:ValueError: invalid literal for int() with base 10: ''
但是'8'没有任何小数。它也没有任何空格。为什么这不起作用?当然可以用 3 表示 8。
Why isn't this working?
因为8是3进制的无效整数,3进制只允许0(含)到2(不含)的整数,所以造成ValueError
.
Surely 8 is possible to represent in base 3.
是的,但这不是 int()
所做的。它将表示形式转换为数字。
>>> int('22', 3)
8
如上所示,int
接受 in 指定基数中的一个数字,然后将其转换为整数。
为什么会这样?:
>>> int('20', 3)
6
>>> int('8', 3)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
int('8', 3)
ValueError: invalid literal for int() with base 3: '8'
是的,我看过这个post:ValueError: invalid literal for int() with base 10: ''
但是'8'没有任何小数。它也没有任何空格。为什么这不起作用?当然可以用 3 表示 8。
Why isn't this working?
因为8是3进制的无效整数,3进制只允许0(含)到2(不含)的整数,所以造成ValueError
.
Surely 8 is possible to represent in base 3.
是的,但这不是 int()
所做的。它将表示形式转换为数字。
>>> int('22', 3)
8
如上所示,int
接受 in 指定基数中的一个数字,然后将其转换为整数。