[英]Unknown format code 'b' for object of type 'str'
def reverseBits(self, n):
binary = '{0:08b}'.format(n)
rev = reversed(binary)
print(rev)
return rev
I get the error: 我得到错误:
ValueError: Unknown format code 'b' for object of type 'str' ValueError:类型为“ str”的对象的未知格式代码“ b”
It says that this error occurs on line 20 even though there is no line 20. 它说即使没有第20行,此错误也会在第20行发生。
What am I doing wrong here? 我在这里做错了什么?
I believe you are trying to convert a string to binary. 我相信您正在尝试将字符串转换为二进制。 First it must be converted to Int.
首先,必须将其转换为Int。 What I mean is, say you are trying to get the reverse bits of the number
16
. 我的意思是说您正在尝试获取数字
16
的反位。 In Base 2, 16 is 10000
and Reverse that is 00001
. 在Base 2中,16是
10000
,反向是00001
。 The parameter you pass to the .format
Must be of type int. 传递给
.format
的参数必须为int类型。 Dont do (...).format("16")
instead do (...).format(16)
不要做
(...).format("16")
而是做(...).format(16)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.