Search code examples
pythonbinarydecimalconverters

denary/binary converter cannot convert binary


I have been attempting to create a binary to denary converter and vice versa. Though the denary to binary works fine, the binary to denary just spits out the same input as I put into it... My code looks like this:

a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
b = []
c = []


while a != 3:
    if a == 1:
        print("You have selected denary to binary.")
        b = int(input("Enter the denary number you want to convert into binary: "))
        if type(b) == int:
            print("Equivalent binary number: ", bin(b))
            a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
        elif type(b) != int:
            print("sorry, your input is not convertible to binary")
        else:
            a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
    elif a == 2:
        print("You have selected binary to denary.")
        c = int(input("Enter the binary number you want to convert to denary: "))
        if type(c) == int:
            cbin = bin(c)
            print("Equivalent denary number: ", int(cbin, 2))
            a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
        elif type(c) != int:
            print("sorry, your input is not convertible to denary")
        else:
            a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
    else:
        print("invalid input, please try again.")
        a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))

My output looks like this:

Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit...1

You have selected denary to binary. Enter the denary number you want to convert into binary: 25

Equivalent binary number: 0b11001

Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit...2

You have selected binary to denary. Enter the binary number you want to convert to denary: 11001

Equivalent denary number: 11001

... any advice would be great, I'm using int(X, 2) to convert binary to denary, but as you can see it's just not working.


Solution

  • a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
    b = []
    c = []
    
    
    while a != 3:
        if a == 1:
            print("You have selected denary to binary.")
            b = int(input("Enter the denary number you want to convert into binary: "))
            if type(b) == int:
                print("Equivalent binary number: ", bin(b))
                a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
            elif type(b) != int:
                print("sorry, your input is not convertible to binary")
            else:
                a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
        elif a == 2:
            print("You have selected binary to denary.")
            c = int(input("Enter the binary number you want to convert to denary: "))
            if type(c) == int:
                cbin = str(c)
                print("Equivalent denary number: ", int(cbin, 2))
                a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
            elif type(c) != int:
                print("sorry, your input is not convertible to denary")
            else:
                a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
        else:
            print("invalid input, please try again.")
            a = int(input("Enter 1 for denary into binary, 2 for binary into denary, or 3 to quit..."))
    

    This line was causing problem:

    cbin = bin(c)
    

    Change it to this:

    cbin = str(c)
    

    Your program is not handling any type of exception, such as, if user inputs: 112 for binary, then it will raise error. I would suggest you to add exception handling too.