Patrick B. answered 01/13/21
Math and computer tutor/teacher
You can use the for loop because you know the length of the input string...
def bin2dec(bitStr):
strLen = len(bitStr)
expo = int(strLen)-1
sum=0
for i in range(int(strLen)):
if (bitStr[i]=='1'):
sum = int(sum) + pow(2,expo)
expo=int(expo)-1
return(sum)
#ASSUMES the user will input a pure bit string and not garbage
bitStr = input("Please input binary string:>")
print(bin2dec(bitStr))