我不能让我的二进制加法工作

时间:2014-02-25 13:49:19

标签: python binary

这需要消除领先的零点&实际添加数字

基本上我需要2个输入,只需要8位二进制。我将它们分成列表,并将每个数字列表添加到另一个列表中的其中。然后我打印输出。它排除了某些事情并检查二进制文件是否相关。

# Variables
no1 = ''
no2 = ''
splitList1 = []
splitList2 = []
actualList = []
overflow = 0
def start(actualList, overflow):
    print("Welcome to the Binary Addition Facility!")
    no1 = input("1st Number please: ")
    no2 = input("2nd Number please: ")
    checks(no1, no2, actualList, overflow)

def checks(no1, no2, actualList, overflow):
    splitList1 = list(str(no1))
    splitList2 = list(str(no2))
    # -- Checks there's no letters --
    for i in splitList1:
        if splitList1[int(i)].isdigit():
            pass

        else:
            raise ValueError

    for i in splitList2:
        if splitList2[int(i)].isdigit():
            pass

        else:
            raise ValueError

    # -- Makes sure it's not huge -- 
    if int(len(str(no1))) > 8:
        raise ValueError

    if int(len(str(no2))) > 8:
        raise ValueError

#    # -- Makes sure it's 8-bit --
 #   if len(str(no1)) < 8 and input("Do you want to convert this number to 8-bit or not? (y/n): ").lower() == 'y' or len(str(no2)) < 8 and input("Do you want to convert this number to 8-bit or not? (y/n): ").lower():
  #      for i in range(0, 8 - int(len(splitList1))):
   #         splitList1.insert(0, 0)
#
 #       for i in range(0, 8 - int(len(splitList2))):
  #          splitList2.insert(0, 0)
#
 #   else:
  #      pass

    # -- Only accept 0's and 1's --
    for i in splitList1:
        if int(splitList1[int(i)]) == 0 or int(splitList1[int(i)]) == 1:
            print("Number 1: " + str(i) + ' in range')
        else:
            raise ValueError
    print("#####################")

    for i in splitList2:
        if int(splitList1[int(i)]) == 0 or int(splitList1[int(i)]) == 1:
            print("Number 2: " + str(i) + ' in range')
        else:
            raise ValueError

    actual(splitList1, splitList2, actualList, overflow)

def actual(splitList1, splitList2, actualList, overflow):
    ########## ACTUAL ADDITION ##########
    for i in range(0, 7):
#Adds the two digit and the overflow
        temp = int(splitList1[8 - int(i)]) + int(splitList2[8 - int(i)]) + int(overflow)
# Puts it into a list at the start
        actualList.insert((8 - int(i)), temp)
# Gets rid of overflow
        overflow = 0
# Checks if that number is 2
        if int(actualList[8 - int(i)]) == 2:
# Deletes it and replaces it with 0 with 1 overflow
            del actualList[8 - int(i)]
            actualList.insert(8 - int(i), 0)
            overflow = 1

# Checks if that number is 3 - Does the same but with a 1
        elif int(actualList[8 - int(i)]) == 3:
            del actualList[8 - int(i)]
            actualList.insert(8 - int(i), 1)
            overflow = 1

        else:
            pass

    print(actualList)

### Exectution and Catch ###
try:
    start(actualList, overflow)
except ValueError:
    print("Error Value Invalid: Looping")
    start(actualList, overflow)
#except IndexError:
#    print("Overflowed!")
    start(actualList, overflow)
except KeyboardInterrupt:
    print("Please refrain from pressing keys")

2 个答案:

答案 0 :(得分:1)

您可以使用lstrip()功能删除主要字符,即number.lstrip('0')

答案 1 :(得分:0)

这样的东西
s = '0012300'
while s[0] == '0':
    s = s[1:]
print(s)

输出

12300

我不是Python专家,这只是我的头脑,所以我确信还有其他(和更好的)方法。