Advertisement
Stewie410

BinaryConverter_Modified.py

Aug 5th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. # Binary-Conversion revised.
  2. decimal = 0
  3. binary = 0
  4. isValid = True
  5.  
  6. binary = input('Enter an unsigned binary number: ')
  7. if binary:
  8.     if binary.isnumeric():
  9.         binary = int(binary)
  10.         tmp = binary
  11.         i = 1
  12.         while tmp > 0:
  13.             digit = tmp % 10
  14.             if ((digit < 0) or (digit > 1)):
  15.                 print('Error, input contains non-binary digits: ' + str(digit))
  16.                 isValid = False
  17.                 break
  18.             else
  19.                 print('Processing: ' + (str(digit)))
  20.                 decimal = decimal + (digit * i)
  21.                 tmp = int(tmp / 10)
  22.                 i = i * 2
  23.         if isValid:
  24.             print('Bin: ' + str(binary))
  25.             print('Dec: ' + str(decimal))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement