gpt4 book ai didi

python - 我无法让我的二进制加法工作

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:28 28 4
gpt4 key购买 nike

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

基本上我采用 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")

最佳答案

您可以使用lstrip()函数删除前导字符,即number.lstrip('0')

关于python - 我无法让我的二进制加法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016126/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com