[英]How would I fix the Python Binary Addition bug in my code
大家好,這是我編寫的代碼。 但是,它確實按預期工作,而不是輸出轉換后的二進制數,而是每次打印0。 有人請協助我解決此問題。 謝謝
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
此行和另一個類似的錯誤原因:
decnum = decnumstored+int(decnum) #stores result of bin2dec2 in the decnumstored1 var.
您未按照注釋中的decnumstored1
將結果存儲在decnumstored1
中。
好消息是您的評論使其他人很容易找到該錯誤。
有很多代碼,我不認為是否有人願意為您調試它,所以不是。 但我可以建議您使用一種更簡單的方法來查找二進制數之和:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.