質問

So im trying to create a simple program that will add two binary strings together, im almost done but the output it creates is reversed, how would i implement a way to reverse it in my code? I want to remove the "ob" at the start of the output as well.

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
役に立ちましたか?

解決

You could just reverse the string using:

ans = ans[::-1]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top