In your code, simply remove this line as you are immediately reassigning over the functions input argument
binary = []
As a more concise alternative
def binatodeci(binary):
return sum(val*(2**idx) for idx, val in enumerate(reversed(binary)))
Example
>>> binatodeci([1,1,0,1])
13
Answer from Cory Kramer on Stack OverflowIn your code, simply remove this line as you are immediately reassigning over the functions input argument
binary = []
As a more concise alternative
def binatodeci(binary):
return sum(val*(2**idx) for idx, val in enumerate(reversed(binary)))
Example
>>> binatodeci([1,1,0,1])
13
As stated in Cory Kramer's answer, simply remove the line binary = [] at the beginning of your code and it will work perfectly.
Here is an alternative algorithm, given as food for thought:
def binatointeger(binary):
number = 0
for b in binary:
number = (2 * number) + b
return number
Important note
Note that the resulting number is not in decimal. It's a python integer. Internally in the computer, python integers are stored in binary. The conversion to decimal representation, i.e., to a string of digits, happens when calling the function print, str, or repr, to print the number to screen.