0

こんにちは、これは私が書いたコードです。ただし、毎回 0 を出力する変換された 2 進数を出力する代わりに、意図したとおりに機能します。誰かがこれを修正するのを手伝ってください。ありがとう

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
4

2 に答える 2

1

この行と別の同様の行がバグの原因です:

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

decnumstored1コメントで説明されているように、結果を保存していません。

幸いなことに、あなたのコメントにより、他の誰かがバグを見つけやすくなりました。

于 2013-06-29T14:29:27.340 に答える
0

多くのコードがあり、誰かがあなたのためにそれをデバッグしたいと思っているとは思いません.SOはそうではありません. しかし、2 進数の合計を見つけるためのより簡単な方法を提案できます。

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
于 2013-06-29T13:27:33.707 に答える