我如何修复代码中的Python二进制加法错误

时间:2013-06-29 12:50:07

标签: python python-2.7

大家好,这是我写的代码。但是,它确实按预期工作,而不是每次输出它打印0的转换二进制数。有人请协助我解决这个问题。感谢

import time
import sys
n=0
while n!=1:
    error=True
    error1=True
    print"\n"
    print"                  *************************************"
    print"                  *!Welcome to RBDC BinAdd Calculator!*"
    print"                  *+++++++++++++1-Bin_Add+++++++++++++*"
    print"                  *+++++++++++++++2-Exit++++++++++++++*"
    print"                  *************************************"
    print"                              Build 0.0.9 ALPHA\n"

    while error:
        try:
            choice=input("Select a Option: ")
            print "\n"
            if choice >=3:
                print"Please enter a number between 1-2."
            error=False
        except NameError:
            print"Not a number. Please try again."
            time.sleep(1)
        except SyntaxError:
            print"Not a number. Please try again."
            time.sleep(1)

    if choice ==1:
        print"***You have selected Bin_Add.***\n"
        while error1:
            try:
                bin2dec = raw_input("Please enter 1st binary number: ")
                bin2dec2 = raw_input("Please enter 2nd binary number: ")
                error1=False
            except NameError:
                print"Enter a Binary number. Please try again.\n"
                time.sleep(0.5)
            except SyntaxError:
                print"Enter a Binary number. Please try again.\n"
                time.sleep(0.5)

        time.sleep(1)
        if False in [i == '0' or i == '1' for i in bin2dec]: # check if the number is in Binary for bin2dec
            print "\n1st input is not Binary number. Please try again."
            time.sleep(1)
        else:
            print "\n1st input is a Binary number"

        time.sleep(1)
        if False in [i == '0' or i == '1' for i in bin2dec]: # check if the number is in Binary for bin2dec2
            print "\n2nd input is not Binary number. Please try again."
            time.sleep(1)
        else:
            print "2nd input is a Binary number"

            decnumstored=0
            decnum = 0
            for i in bin2dec:
                decnum = decnum * 2 + int(i)
                time.sleep(0.25)
                decnum = decnumstored+int(decnum)  #stores result of bin2dec2 in the decnumstored1 var.

            decnumstored1=0
            decnum1 = 0
            for i in bin2dec2:
                decnum = decnum1 * 2 + int(i)
                time.sleep(0.25)
                decnum = decnumstored1+int(decnum1)  #stores result of bin2dec2 in the decnumstored1 var.

            a=decnumstored+decnumstored1  ##adds the 2 variables and converts to binary
            b = ''            
            b = str(a % 2) + b
            a >>= 1
            print "\n",str(b),"<<This is your answer!"  ##          



    if choice==2:
        endex=raw_input("\nDo you want to Exit? \nInput Y or N: ")
        if endex == "N":
            print "You have chosen to run this programme again...\n"
        elif endex == "n":
            print "You have chosen to run this programme again...\n"
        else:
            print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
            time.sleep(2)
            break

2 个答案:

答案 0 :(得分:1)

这一行和另一个类似的是导致错误的原因:

decnum = decnumstored+int(decnum)  #stores result of bin2dec2 in the decnumstored1 var.

您没有按照评论中的说明将结果存储在decnumstored1中。

好消息是,您的评论让其他人很容易找到错误。

答案 1 :(得分:0)

有很多代码,我不认为是否有人想为你调试它,所以不是为了它。但我建议你更容易找到二进制数的总和:

def sum_binary_numbers(l):
    return bin(sum(map(lambda x: int(x, 2), l)))

并且这样打电话:

try:
    print sum_binary_numbers(map(raw_input, ["First: ", "Second: "]))
except ValueError:
    print "Incorrect numbers. Try again"

演示:

In [15]: try:
   ....:     print sum_binary_numbers(map(raw_input, ["First: ", "Second: "]))
   ....: except ValueError:
   ....:     print "Incorrect numbers. Try again"
   ....:     
First: 10101
Second: 1010
0b11111

In [16]: try:
   ....:     print sum_binary_numbers(map(raw_input, ["First: ", "Second: "]))
   ....: except ValueError:
   ....:     print "Incorrect numbers. Try again"
   ....:     
First: 42
Second: Some words
Incorrect numbers. Try again