[ I can't get my Binary Addition to work ]
THIS NEEDS TO REMOVE LEADING ZEROS & ACTUALLY ADD THE NUMBERS
Basically I take 2 inputs that only need to be 8-bit binary. I split them into lists and add each digit list to its twin in the other list. I then print the output. It excepts certain things and checks the binary is relevant.
# 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")
Answer 1
How about something like
s = '0012300'
while s[0] == '0':
s = s[1:]
print(s)
Output
12300
I'm no Python expert, this was just of the top of my head, so I'm sure there are other (and better) ways.
Answer 2
You can use the lstrip()
function to remove leading characters, i.e. number.lstrip('0')
.