1

I'm trying to add two binary numbers in python using a simple code. Can someone tell me why this isn't working? Thanks so much! I tried breaking the code down but when I break it down it all works separately. But of course when I put it together it doesn't work.

I'm a beginner in Python so explanations will certainly be appreciated. Thanks for the help!

#Enter numbers and define variables
number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")
x1 = number1[0]
x2 = number1[1]
y1 = number2[0]
y2 = number2[1]

#Convert to integers from strings
int(x1)
int(x2)
int(y1)
int(y2)



#Digit 1 of answer and digit to be carried
if x1+y1==0:
  a1 = 0
  c1 = 0
if x1+y1==1:
  a1 = 1
  c1 = 0
if x1+y1==2:
  a1 = 0
  c1 = 1




#Digit 2 of answer and digit to be carried
if x2+y2+c1==0:
  a2=0
  c2=0
if x2+y2+c1==1:
  a2=1
  c2=0
if x2+y2+c1==2:
  a2=0
  c2=1
if x2+y2+c1==3:
  a2=1
  c2=1

#Digit 3 of answer
c2=a3

if a3==1:
  print ("a3" + "a2" + "a1")

if a3==0:
  print ("a2" + "a1")

The error it's giving is that c1 and a1 aren't defined.

Thanks again!

1
  • 1
    I would suggest initializing your variables near the top of the function to something. You are getting this error because all of the first 3 if statements are false, and therefore a1 and c1 never defined. Commented Jun 28, 2017 at 4:22

4 Answers 4

3

I will suggest simple code to add two binary numbers.

number1 = str(input("Enter 1st Binary Number"))
number2 = str(input("Enter 2nd Binary Number"))
intSum = int(number1, 2) + int(number2, 2)  #int() takes first argument as string
result = bin(intSum)[2:]  #remove 0b
print result

output

Enter 1st Binary Number1111
Enter 2nd Binary Number1010
11001
Sign up to request clarification or add additional context in comments.

Comments

1

Change

#Convert to integers from strings
int(x1)
int(x2)
int(y1)
int(y2)

to

#Convert to integers from strings
x1 = int(x1)
x2 = int(x2)
y1 = int(y1)
y2 = int(y2)

Also you have not defined a3:

#Digit 3 of answer
c2=a3

If you want to print the value of the variables (might have to convert them to strings using str()), use

if a3==1:
  print (a3 + " " + a2 + " " + a3)

if a3==0:
  print (a2 + " " + a1)

If you want to add the variables, use:

if a3==1:
  print (a3 + a2 + a1)

if a3==0:
  print (a2 + a1)

instead of

if a3==1:
  print ("a3" + "a2" + "a1")

if a3==0:
  print ("a2" + "a1")

2 Comments

Thank you so much, this was so helpful! I really appreciate it!
sure no problem, If it solved your problem, please check this as answer so that it helps the rest of the SO community :)
1

There are several ways in python to add two binary numbers and print them. Below are the methods. (However I will be explaining your problem below)

METHOD 1:

number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")

num1 =int(number1,2)  //Convert string to binary number **NOTE:** int('string',base) is the format
num2 = int(number2,2)

bin_num = bin(num1+num2)
print(bin_num)
print(str(bin_num)[2:] #just to remove the '0b'

output:

Enter 1st Binary Number:10
Enter 2nd Binary Number:01
0b11
11

METHOD 2:

number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")

num1 =int(number1,2)
num2 = int(number2,2)
print("{0:b}".format(num1+num2))

output:

Enter 1st Binary Number:10
Enter 2nd Binary Number:01
0b11
11

YOUR PROBLEM:

You can declare them at the top of your program and then use them.

Also you have one more thing wrong,

int(x1)
int(x2)
int(y1)
int(y2)

These four lines do nothing. You are not changing the values in x1,x2,y1,y2 you are just calling int() them without modifying them. Do this instead,

x1=int(x1)
x2=int(x2)
x3=int(y1)
x4=int(y2)

Complete code:

a1=c1=a2=c2=a3=c3=0 #Declare and define them all `0`s at top
#Enter numbers and define variables
number1=input("Enter 1st Binary Number:")
number2=input("Enter 2nd Binary Number:")
x1 = number1[0]
x2 = number1[1]
y1 = number2[0]
y2 = number2[1]

#Convert to integers from strings
x1=int(x1)
x2=int(x2)
x3=int(y1)
x4=int(y2)



#Digit 1 of answer and digit to be carried
if x1+y1==0:
  a1 = 0
  c1 = 0
if x1+y1==1:
  a1 = 1
  c1 = 0
if x1+y1==2:
  a1 = 0
  c1 = 1




#Digit 2 of answer and digit to be carried

if x2+y2+c1==0:
  a2=0
  c2=0
if x2+y2+c1==1:
  a2=1
  c2=0
if x2+y2+c1==2:
  a2=0
  c2=1
if x2+y2+c1==3:
  a2=1
  c2=1

#Digit 3 of answer
c2=a3


if a3==1:
  print (a3 , a2, a1,sep='')

if a3==0:
  print (a2 , a1,sep='')

And finally the last lines won't print the values of a1,a2 instead it will only print the strings. so remove the double quotes.

NOTE: I have given an extra parameter sep='' without it your output will have spaces between them. Like,

1 1

After sep='' you will get

11

1 Comment

Thank you for the help! Also, I believe defining them in the if statement doesn't make them local. In Python, variables defined in if statements are global.
0

Every other answers are correct. Few other points though.

1.You can combine conversion to integer and assignment at one step. Like:

x1 = int(number1[0])
x2 = int(number1[1])
y1 = int(number2[0])
y2 = int(number2[1])

2.While doing multiple conditional checks with the same variable set, it's a good idea to use elif if it is not harming your logic. In your case:

#Digit 1 of answer and digit to be carried
if x1+y1==0:
  a1 = 0
  c1 = 0
elif x1+y1==1:
  a1 = 1
  c1 = 0
elif x1+y1==2:
  a1 = 0
  c1 = 1

Because, x1+y1==0, x1+y1==1, x1+y1==2 are not going to be true simultaneously. Similarly it can be applied for the digit 2 of the answer also.

3.The number of lines can be decreased with a pre-assignment.

a1 = c1 = 0

Then your digit 1 block will look like:

#Digit 1 of answer and digit to be carried
if x1+y1==1:
  a1 = 1
elif x1+y1==2:
  c1 = 1

Here, I removed the checking for x1+y1==0, because by default a1 and c1 are 0. And for other invalid cases, you have to make another conditional statement. Digit 2 block also can be modified similarly.

2 Comments

Thank you so much!
Welcome to Stackoverflow. Avoid commenting like Thank you or +1 or something like that. If an answer is useful, simply upvote it. Again, welcome aboard.

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.