denary/binary转换器无法转换二进制

denary/binary converter cannot convert binary

我一直在尝试创建二进制到二进制转换器,反之亦然。虽然 denary to binary 工作正常,但 denary to denary 只是吐出与我输入的输入相同的输入......我的代码如下所示:

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..."))

我的输出如下所示:

输入 1 将二进制转换为二进制,输入 2 将二进制转换为二进制,或输入 3 退出...1

您选择了十进制转二进制。 输入要转换成二进制的十进制数:25

等效二进制数:0b11001

输入 1 将二进制转换为二进制,输入 2 将二进制转换为二进制,或输入 3 退出...2

您选择了二进制来拒绝。 输入要转十进制的二进制数:11001

等值密林数:11001

...任何建议都很好,我正在使用 int(X, 2) 将二进制转换为 denary,但如您所见,它只是行不通。

此处 int(input("Enter the binary number you want to convert to denary: ")) 您将数字解析为十进制。在这里将 2 作为第二个参数传递给 int 以将其解析为二进制。

n = int(input("Enter the binary number you want to convert to denary: "), 2)
print("Equivalent denary number:", n)

打印时无需将数字转换为十进制:默认情况下数字以十进制打印。

顺便说一下,您所有的 type(c) == int 检查都是多余的。您已经将输入转换为 int,因此除非引发异常,否则 c 绝对是一个 int。

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..."))

这条线引起了问题:

cbin = bin(c)

改成这样:

cbin = str(c)

您的程序未处理任何类型的异常,例如,如果用户输入:二进制 112,则会引发错误。我建议您也添加 异常处理