Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. x = raw_input("type a for decimal to binary, or b for binary to decimal: ")
  2.  
  3. if x == 'a':
  4.     while True:
  5.         try:
  6.             n = int(raw_input("Enter your number, between 0 and 250: "))
  7.             if 0 <= n <= 250:
  8.                 print n,"in binary is: {:08b}".format(n)
  9.                 break
  10.                
  11.             else:
  12.                 print n, "not in range, please try again.."
  13.                
  14.         except ValueError:
  15.             print "invalid data found"
  16.  
  17. elif x == 'b':
  18.     num2 = raw_input("Enter your number at most 8 digits long: ")
  19.  
  20.     while True:
  21.         try:
  22.             decimal = int(num2, 2)
  23.  
  24.             if len(num2)>8:
  25.                 print "binary number is too large"
  26.  
  27.             #got a valid binary number ==> terminate and exit loop
  28.             else:  break
  29.  
  30.         except ValueError:
  31.             print "Data entered is not a binary number"
  32.    
  33.         num2 = raw_input("please re-enter binary number: ")
  34.  
  35.     print "{0:08d} in decimal is: {1}".format(int(num2),decimal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement