1

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}')
1
  • Use enumerate to have a correct index. Also, not digit ** exponent, you need digit * 2 ** exponent. Commented Mar 31, 2022 at 5:14

3 Answers 3

2

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))))
answered Mar 31, 2022 at 5:18
Sign up to request clarification or add additional context in comments.

Comments

0

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}')
answered Mar 31, 2022 at 5:24

Comments

0

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}')
answered Mar 31, 2022 at 5:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.