Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''How to read a number from the user and convert to decimal'''
- '''Ask user to enter an 8 digit binary number'''
- import sys
- binary = input ("please enter an 8 digit binary number: ")
- '''Checking for binary length'''
- if len(binary)>8 or len(binary)<8:
- print ('Please enter an 8-bit binary number')
- sys.exit
- # checking for valid binary digits
- d = 0
- count = 0
- for d in binary:
- if d =='0' or d == '1':
- continue
- else:
- print('Binary number must contain combinations of 1s and 0s.' )
- sys.exit()
- # converting binary to decimal
- d = 0
- count = 0
- for b in binary:
- d = d + int (b) * (2 ** count)
- count = count + 1
- # displaying the equivalent decimal number
- print('The decimal equivalent of the binary number is: ' (d))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement