简体   繁体   English

类型“ str”的对象的未知格式代码“ b”

[英]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.

相关问题 “str”类型对象的未知格式代码“g” - Unknown format code 'g' for object of type 'str' ValueError:类型为“ str”的对象的未知格式代码“ g” - ValueError: Unknown format code 'g' for object of type 'str' ValueError: 类型“str”的对象的格式代码“e”未知 - ValueError: Unknown format code 'e' for object of type 'str' ValueError:类型为“str”的 object 的未知格式代码“f” - ValueError: Unknown format code 'f' for object of type 'str' 类型为'str'的对象的未知格式代码'f'-Folium - Unknown format code 'f' for object of type 'str'- Folium 卡在 python 代码中进行加密。 错误是“str”类型的 object 的未知格式代码“x” - stuck in a python code for encryption. error is Unknown format code 'x' for object of type 'str' 在代码中格式化不断导致 ValueError: Unknown format code 'f' for object of type 'str'? - Formatting in a code constantly causes the ValueError: Unknown format code 'f' for object of type 'str'? ValueError:类型为'str'的对象的未知格式代码'f'-为什么第二次却不是第一次? - ValueError: Unknown format code 'f' for object of type 'str' - why do I get this the second time but not the first time? 在我的情况下,“str”类型的 object 的“未知格式代码 'f' 是什么意思? - What does "Unknown format code 'f' for object of type 'str' mean in my case? Rc4 decrypt ValueError:object 类型为“str”的未知格式代码“x” - Rc4 decrypt ValueError: Unknown format code 'x' for object of type 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM