十进制/二进制转换器无法转换二进制

Posted

技术标签:

【中文标题】十进制/二进制转换器无法转换二进制【英文标题】:denary/binary converter cannot convert binary 【发布时间】:2021-07-29 18:31:19 【问题描述】:

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

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

您已选择要拒绝的二进制文件。 输入要转换为denary的二进制数:11001

等效拒绝数:11001

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

【问题讨论】:

这里 int(input("Enter the binary number you want to convert to denary: ")) 您将数字解析为十进制。在这里调用int 时使用base 参数将其解析为二进制。 【参考方案1】:

这里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。

【讨论】:

【参考方案2】:
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 表示二进制,那么它将引发错误。我建议您也添加异常处理

【讨论】:

以上是关于十进制/二进制转换器无法转换二进制的主要内容,如果未能解决你的问题,请参考以下文章

16进制字符串如何转化16进制数值

Silverlight 中的十进制转换器无法正确处理丢失焦点

将十进制转换为二进制向量

Spark 案例类 - 十进制类型编码器错误“无法从十进制向上转换”

将整数编码转换为二进制编码需要啥?

将十进制转换为二进制的程序不适用于大输出