Python中十进制到二进制的转换,反之亦然 给定一个十进制数,我需要把它转换成二进制 给定一个二进制数,我需要把它转换成十进制

Python中十进制到二进制的转换,反之亦然 给定一个十进制数,我需要把它转换成二进制 给定一个二进制数,我需要把它转换成十进制,python,binary,decimal,Python,Binary,Decimal,转换后,我需要对其执行一些操作(例如,添加)。我还需要打印指定宽度的结果 为了完成上述任务,我编写了以下代码: 二进制十进制: 十进制二进制: 你想知道还有其他(更好的)方法吗?我知道可以使用bin() 另外,任何改进代码的注释都将非常感谢,因为我是Python的初学者 这似乎是在做我想做的事。下面的代码是基于我在问题中给出的原始代码以及来自的建议构建的 #!/usr/bin/python3 # this function accepts a decimal integer and ret

转换后,我需要对其执行一些操作(例如,添加)。我还需要打印指定宽度的结果

为了完成上述任务,我编写了以下代码:

二进制十进制:

十进制二进制:

你想知道还有其他(更好的)方法吗?我知道可以使用
bin()


另外,任何改进代码的注释都将非常感谢,因为我是Python的初学者

这似乎是在做我想做的事。下面的代码是基于我在问题中给出的原始代码以及来自的建议构建的

#!/usr/bin/python3

# this function accepts a decimal integer and returns a binary string
def decimalToBinary (decimalInt) :
    return "{0:b}".format (decimalInt)
# this function accepts a binary string and returns a decimal integer
def binaryToDecimal (binaryString) :
    return int (binaryString, 2)

x = int (input ("Enter a decimal number: "))
y = int (input ("Enter a decimal number: "))
sumInDecimal = x + y
print ("{0} when converted to binary is {1}".format (x, decimalToBinary(x)))
print ("{0} when converted to binary is {1}".format (y, decimalToBinary(y)))
print ("Addition in base 10: \t {0} + {1} = {2}".format (x, y, x + y))
print ("Addition in base  2: \t {0} + {1} = {2}".format (decimalToBinary(x), decimalToBinary(y), decimalToBinary(sumInDecimal)))
print ()
x = input ("Enter a binary number: ")
y = input ("Enter a binary number: ")
# convert binary to decimal before any operation, the result of the operation will be in decimal
sumInDecimal = binaryToDecimal(x) + binaryToDecimal(y)
print ("{0} when converted to decimal is {1}".format (x, binaryToDecimal(x)))
print ("{0} when converted to decimal is {1}".format (y, binaryToDecimal(y)))
print ("Addition in base  2: \t {0} + {1} = {2}".format (x, y, decimalToBinary(sumInDecimal)))
print ("Addition in base 10: \t {0} + {1} = {2}".format (binaryToDecimal(x), binaryToDecimal(y), sumInDecimal))

如果这是您认为可以改进的工作代码,请参阅。一个明显的问题是,您要求用户输入二进制,然后立即将其转换为整数作为十进制输入,这似乎适得其反
    n1 = int(input("Enter a decimal number: "))
    n2 = int(input("Enter a decimal number: "))

    # type cast to 'int' is done to provide width
    binaryN1 = int("{0:b}".format(n1))
    binaryN2 = int("{0:b}".format(n2))
    binarySum = int("{0:b}".format(n1 + n2))

    width = (n1 + n2).bit_length()
    print("max width = {}".format(width))

    print ("{0:0{3}} + {1:0{3}} = {2:0{3}}".format(binaryN1, binaryN2, binarySum, width))
    print ("{0} + {1} = {2}".format(type(binaryN1), type(binaryN2), type(binarySum)))
#!/usr/bin/python3

# this function accepts a decimal integer and returns a binary string
def decimalToBinary (decimalInt) :
    return "{0:b}".format (decimalInt)
# this function accepts a binary string and returns a decimal integer
def binaryToDecimal (binaryString) :
    return int (binaryString, 2)

x = int (input ("Enter a decimal number: "))
y = int (input ("Enter a decimal number: "))
sumInDecimal = x + y
print ("{0} when converted to binary is {1}".format (x, decimalToBinary(x)))
print ("{0} when converted to binary is {1}".format (y, decimalToBinary(y)))
print ("Addition in base 10: \t {0} + {1} = {2}".format (x, y, x + y))
print ("Addition in base  2: \t {0} + {1} = {2}".format (decimalToBinary(x), decimalToBinary(y), decimalToBinary(sumInDecimal)))
print ()
x = input ("Enter a binary number: ")
y = input ("Enter a binary number: ")
# convert binary to decimal before any operation, the result of the operation will be in decimal
sumInDecimal = binaryToDecimal(x) + binaryToDecimal(y)
print ("{0} when converted to decimal is {1}".format (x, binaryToDecimal(x)))
print ("{0} when converted to decimal is {1}".format (y, binaryToDecimal(y)))
print ("Addition in base  2: \t {0} + {1} = {2}".format (x, y, decimalToBinary(sumInDecimal)))
print ("Addition in base 10: \t {0} + {1} = {2}".format (binaryToDecimal(x), binaryToDecimal(y), sumInDecimal))