0

Hi everyone this is the code I wrote. However, it does work as intended instead of outputting the converted binary number it prints 0 every time. Someone please assist me in fixing this. Thanks

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 Answers 2

1

This line and another similar are the cause of the bug:

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

You are not storing the result in decnumstored1 as described in your comment.

The good news is that your commenting made it easy for someone else to find the bug.

Sign up to request clarification or add additional context in comments.

Comments

0

There is a lot of code and I don't think if anybody wants to debug it for you, SO is not for it. But I can suggest you a more easier way to find a sum of binary numbers:

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

And call like this:

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

Demo:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.