我无法得到二进制加法

2024-09-28 15:10:26 发布

您现在位置:Python中文网/ 问答频道 /正文

这需要删除前导零并实际添加数字

基本上我需要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")

Tags: inforifelseintraiseprintvalueerror
2条回答

像这样的怎么样

s = '0012300'
while s[0] == '0':
    s = s[1:]
print(s)

输出

12300

我不是Python专家,这只是我的想法,所以我确信还有其他更好的方法。你知道吗

可以使用lstrip()函数删除前导字符,即number.lstrip('0')。你知道吗

相关问题 更多 >