Python 如何在二进制加法器中反转输出?

Python 如何在二进制加法器中反转输出?,python,binary,Python,Binary,因此,我试图创建一个简单的程序,将两个二进制字符串添加到一起,我几乎完成了,但它创建的输出是反向的,我将如何在代码中实现反向的方法?我还想删除输出开头的“ob” repeat = True while repeat==True: num1 = input("what number would you like to add ") if num1.isdigit(): a= int(num1) elif not num1.isdigit():

因此,我试图创建一个简单的程序,将两个二进制字符串添加到一起,我几乎完成了,但它创建的输出是反向的,我将如何在代码中实现反向的方法?我还想删除输出开头的“ob”

repeat = True
while repeat==True:

    num1 = input("what number would you like to add ")
    if num1.isdigit():
        a= int(num1)
    elif not num1.isdigit():
        continue
    a = int(num1, 2)

    num2 = input("what number would you like to add to it ")
    if num2.isdigit():
        b= int(num2)
    elif not num2.isdigit():
        continue
    b = int(num2, 2)

    ans = bin(a+b)[2:]
    print("Your addition of numbers, in binary is " + ans)

    choice=input("Would you like to start again, y/n")

    if choice== "y":
        repeat=True
    else:
        print("Its not like i wanted you here or anything baka")
        repeat=False

您可以使用以下方法反转字符串:

ans = ans[::-1]

如果案例:。。。elif not case:…
相同,如果case:。。。其他:…
除了更多的计算-使用后者
isdigit()
也不能保证
int()
能正常工作!而是使用
try:int(…)来捕获异常,ValueError除外:…