【发布时间】:2020-10-05 13:51:20
【问题描述】:
我正在尝试运行这个程序来计算ints 的位表示中的那些。代码如下:
def count_bits(n):
return bin(n).replace("0b", "")
bits = count_bits(3)
for i in bits:
if bits[i] == 1:
counter += 1
else:
counter = counter
但是当我尝试运行它时,我收到以下错误:
if bits[i] == 1:
TypeError: string indices must be integers
【问题讨论】:
-
试试
if bits[int(i)] == 1: -
bits[i]的类型是整数,尝试转换,或者字符串化== '1'
标签: python python-3.x integer bit