Home > Enterprise >  Binary to Decimal Number Converter
Binary to Decimal Number Converter

Time:03-31

Today my professor asked us to do a binary converter using python, and I did it quickly with my friend assigning a variable to the length and decreasing it by 1 in the for loop. And then we decided to do it the opposite way.

We tried to do it converting the number into a list, reversing the list, using the index position to match the required exponent number. But we found 2 main problems with that code:

  • 1.The exponent will never be able to be bigger than 1, because it's not in the list.
  • 2.The number 1 and 0 are going to repeat a lot of times in the list, so there's gonna be a problem when we try to access the index value.

Is there any way to fix it or we should just stick to the other method? Here's the code so you can take a look:

num = input ('Insert the binary number: ')

res = [int(x) for x in num]
res.reverse()
length = len(res)   1
counter = 0

for x in range (0, length):
    if res[x] == 1:
        counter  = res[x]**res.index(x)
    elif res[x] == 0:
        pass
    else:
        print ('The number is not binary')
        break

print (f'Your number in decimal is : {counter}')

CodePudding user response:

Your method is fine. Two errors in your code:

  1. Do not add 1 to the length as it is already the correct value. If you have a, say, 8 digit binary number the range should be from 0 to 7.
  2. You should simply add 2**x to the counter when the digit is 1. I don't know why you tried to use the .index()

Here is a working version:

num = input ('Insert the binary number: ')

res = [int(x) for x in num]
res.reverse()
length = len(res)
counter = 0

for x in range(length):
    if res[x] == 1:
        counter  = 2 ** x
    elif res[x] == 0:
        pass
    else:
        print ('The number is not binary')
        break

print (f'Your number in binary is : {counter}')

...or a one-liner just for some Python fun:

print(sum(int(x) * 2**i for i, x in enumerate(reversed(num))))

CodePudding user response:

For determining the correct value of the exponent you can use enumerated. Also, there is no point in converting the each digit to an integer, you can just compare the string.

num = input('Insert the binary number: ')
result = 0

for i, digit in enumerate(reversed(num)):
    if digit == "1":
        result  = 2 ** i
    elif digit != "0":
        print('The number is not binary')
        break
else:
    print(f'Your number in decimal is : {result}')

CodePudding user response:

Just a little fix and your program will work. Modified length = len(res), added enumerate to find index, and changed the counter formula.

num = input ('Insert the binary number: ')

res = [int(x) for x in num]
res.reverse()
length = len(res)
counter = 0

for index,x in enumerate(range(length)):
    if res[x] == 1:
        counter  = res[x]* 2**index
    elif res[x] == 0:
        pass
    else:
        print ('The number is not binary')
        break

print (f'Your number in decimal is : {counter}')
  • Related