Advertisement
Stewie410

SimpleBinaryToDecimal.py

Aug 4th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. # Binary-Conversion_lu.py
  2. # With some error-handling form apaarfus
  3.  
  4. # Functions
  5. def __isBinary(string):
  6.     if string:
  7.         for i in string:
  8.             if not i.isnumeric():
  9.                 print('Error, contains non-numeric characters.')
  10.                 return False
  11.             elif (int(i) < 0) or (int(i) > 1):
  12.                 print('Error, contains invalid digits.')
  13.                 return False
  14.     else:
  15.         print('Error, nothing was entered.')
  16.         return False
  17.     return True
  18.  
  19. def __binaryToDecimal(string):
  20.         decimal = 0
  21.         for i in string:
  22.             print("Processing: \"" + i + "\"...")
  23.             decimal = decimal*2 + int(digit)
  24.         return decimal
  25.     return -1
  26.        
  27. # Run
  28. strInput = input('Enter a number: ')
  29. if __isBinary(strInput):
  30.     decimal = __binaryToDecimal(strInput)
  31.     print("Binary:  " + strInput)
  32.     print("Decimal: " + str(decimal))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement