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!