Python to Convert Binary to Decimal and Decimal to Binary with Source Code

A small python program that converts Binary to Decimal and Decimal to Binary vice versa.

Prerequisites:

  • Python 3

Run the script:

python decimal_to_binary.py

Code language: CSS (css)

Source Code:

try:
    menu = int(input("Choose an option: \n 1. Decimal to binary \n 2. Binary to decimal\n Option: "))
    if menu < 1 or menu > 2:
        raise ValueError
    if menu == 1:
        dec = int(input("Input your decimal number:\nDecimal: "))
        print("Binary: {}".format(bin(dec)[2:]))
    elif menu == 2:
        binary = input("Input your binary number:\n Binary: ")
        print("Decimal: {}".format(int(binary, 2)))
except ValueError:
    print ("please choose a valid option")

Output:

Leave a Comment