Below you can find a short script on how to convert a binary number to decimal using python casting. Casting is when you convert variable from one type to another. In this case we use python casting to convert string to decimal number that is integer. Therefore, when you attempt to use casting to convert variable type to integer make sure that you variable type imput is a string. Create a following file with a following content:
#!/usr/bin/env python

while True:
    try:
        # Try to convert bunary to decimal
        decimal_num = int(raw_input("Enter a binary number: "), 2)
        # If we fail we ask again user to enter binary number
    except ValueError:
        print "Your input is not a binary number! Please try again."
    else:
        # Exit program if the conversion from binary to decimal was successful
        break
# print converted decimal number
print decimal_num
Save your script as eg. binary2decimal.py, make it executable and execute:
$ chmod +x binary2decimal.py
$ ./binary2decimal.py
how to convert binary to decimal number in python
FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: