I am trying to wrap my head around recursion. I need to convert a binary string of digits into a decimal value.
I've tried to switch around the actual recursion part of my code to no avail. Also, if anyone has seen any good videos or examples of recursion, classes, and inheritance please also link them. These last few concepts in my class have been difficult for me. Thank you.
The homework says the only function I need is the len() function.
def convertToDecimal(binNum):
if len(binNum) < 1:
return int(binNum)
else:
return int(binNum[0])*len(binNum)**2 #+ convertToDecimal(binNum[:-1])
bin = '11111111'
print(convertToDecimal(bin))
I feel like the commented out portion is on the right track but i still get errors.
len()only", since you also useint,*,<,if,else,[:-1]...+= 2 ** len(binNum)in there somewhere. Think about how you would convert binary to decimal without recurrsion...