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

时间:2014-05-09 21:09:41

标签: 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():
        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

1 个答案:

答案 0 :(得分:0)

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

ans = ans[::-1]