Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = raw_input("type a for decimal to binary, or b for binary to decimal: ")
- if x == 'a':
- while True:
- try:
- n = int(raw_input("Enter your number, between 0 and 250: "))
- if 0 <= n <= 250:
- print n,"in binary is: {:08b}".format(n)
- break
- else:
- print n, "not in range, please try again.."
- except ValueError:
- print "invalid data found"
- elif x == 'b':
- num2 = raw_input("Enter your number at most 8 digits long: ")
- while True:
- try:
- decimal = int(num2, 2)
- if len(num2)>8:
- print "binary number is too large"
- #got a valid binary number ==> terminate and exit loop
- else: break
- except ValueError:
- print "Data entered is not a binary number"
- num2 = raw_input("please re-enter binary number: ")
- print "{0:08d} in decimal is: {1}".format(int(num2),decimal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement