python中如何用递归实现二进制转十进制

如题 请问如何用def的方式实现讲一个32位的二进制string转换成十进制的int
不用for和while循环,而用递归的方法

def bintodec(binstr, i=0, count=0):
    if len(binstr) > i:

        if binstr[i] == str(1):
            count += 2 ** (len(binstr) - i - 1)
            i += 1
            return bintodec(binstr, i, count)
        else:
            i += 1
            return bintodec(binstr, i, count)
    else:
        return count


bin = "1011101010101010101010111"
print(bintodec(bin))

有帮助请采纳,有问题继续交流,你的采纳是对我回答的最大的肯定和动力

img