添加二进制数Python

时间:2017-06-28 04:14:59

标签: python

我正在尝试使用简单的代码在python中添加两个二进制数。有人可以告诉我为什么这不起作用?非常感谢!我尝试打破代码,但是当我将其分解时,它们都会分开工作。但当然,当我把它放在一起时它不起作用。

我是Python的初学者,所以解释肯定会受到赞赏。谢谢你的帮助!

#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")

它给出的错误是没有定义c1和a1。

再次感谢!

4 个答案:

答案 0 :(得分:1)

更改

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

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

您还没有定义a3

#Digit 3 of answer
c2=a3

如果要打印变量的值(可能必须使用str()将它们转换为字符串),请使用

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

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

如果要添加变量,请使用:

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

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

而不是

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

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

答案 1 :(得分:1)

python中有几种方法可以添加两个二进制数并打印它们。以下是方法。 (但我会在下面解释你的问题)

方法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'

输出:

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

方法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))

输出:

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

您的问题:

您可以在程序顶部声明它们,然后使用它们。

还有一件事是错误的,

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

这四条线无效。您没有更改x1,x2,y1,y2中的值,只是在不修改它们的情况下调用它们int()。这样做,

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

完整代码:

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='')

最后,最后一行不会打印a1a2的值,而只会打印字符串。所以删除 双引号

注意:我已经给出了一个额外的参数sep='',如果没有它,你的输出之间会有空格。像,

1 1

sep=''之后你会得到

11

答案 2 :(得分:1)

我会建议添加两个二进制数的简单代码。

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

输出

Enter 1st Binary Number1111
Enter 2nd Binary Number1010
11001

答案 3 :(得分:0)

其他答案都是正确的。但是很少有其他观点。

1.您可以将转换结合到整数并分配一步。像:

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

2.虽然使用相同的变量集进行多次条件检查,但如果它不会损害您的逻辑,则最好使用elif。在你的情况下:

#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

因为,x1+y1==0x1+y1==1x1+y1==2不会同时成立。同样,它也可以应用于答案的数字2。

3.通过预分配可以减少行数。

a1 = c1 = 0

然后您的数字1块将如下所示:

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

在此,我删除了对x1+y1==0的检查,因为默认情况下a1c10。而对于其他无效案例,您必须制作另一个条件语句。数字2块也可以类似地修改。