Python: My Return statement always returns None -
this question has answer here:
this function able take in binary number string , convert decimal number. reason, function returns none , can not understand why happening. if provide me explanation appreciated.
total = 0 power = 0 def binarytodecimal(binarystring): global total, power n = len(binarystring) - 1 if n < 0: return total else: if binarystring[n] == '1': total += (2 ** power) power += 1 binarytodecimal(binarystring[:-1])
the final line needs return binarytodecimal(binarystring[:-1])
. @ moment you're returning total
last call, none of preceding calls returning anything.
Comments
Post a Comment