Advertisement
Stewie410

SimpleBinaryToDecimal_Looped.py

Aug 5th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. # For example ONLY
  2.  
  3. binary = 0
  4. decimal = 0
  5. isBinary = True
  6.  
  7. while True:
  8.     binary = input('Enter a number: ')
  9.     if binary:
  10.         for digit in binary:
  11.             if not digit.isnumeric():
  12.                 print('error: not-numeric')
  13.                 isBinary = False
  14.                 break
  15.             elif (int(digit) < 0) or (int(digit) > 1):
  16.                 print('error: some digits < 0 or > 1')
  17.                 isBinary = False
  18.                 break
  19.         if not isBinary:
  20.             continue
  21.     else:
  22.         print('error, no input')
  23.         continue
  24.     break
  25.    
  26.  
  27. for digit in binary:
  28.     print("Processing \"" + digit + "\"...")
  29.     decimal = decimal*2 + int(digit)
  30. print("Binary:  " + binary)
  31. print("Decimal: " + str(decimal))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement