Im relatively new to programming but familiar with the basic concepts of python. My question is this: I've made a program which takes 2 inputs, checks they are less than 8 digits long and only contain 1s and 0s. Once its received and validated its 2 binary inputs it converts them both to denary and adds them together to get a total. Once the total is calculated it converts it back to binary and displays it. I have all the calculations and outputs working but the first bit is causing me hassle. I want to put all the validation of the inputs into a loop so while ever the conditions are not met it continues to ask for an input and does not let any input be accepted which is not in an 8 digit binary form. Ive been trying at this for many days now and finally decided to ask here for help from the experts :P Hope someone can help me, i will be very grateful to further my knowledge on loops. Thankyou, heres my current code :)
valid = 0
while valid == 0 or correctcharacter == 0:
firstnumber = input("First number is...")
valid = 1
correctcharacter = 0
while correctcharacter == 0:
correctcharacter = 1
for number in firstnumber:
if number != "0" and number != "1":
valid = 0
correctcharacter = 0
if len(firstnumber) > 8:
valid = 0
#_________________________________________________________________________________________________________
valid = 0
while valid == 0:
secondnumber = input("Second number is...")
valid = 1
correctcharacter = 0
while correctcharacter == 0:
correctcharacter = 1
for number in secondnumber:
if number != "0" and number != "1":
valid = 0
correctcharacter = 0
if len(secondnumber) > 8:
valid = 0
#_________________________________________________________________________________________________________
multiple = 1
final1 = 0
for number in firstnumber[::-1]:
final1 = final1 + int(number) * multiple
multiple = multiple * 2
multiple = 1
final2 = 0
for number in secondnumber[::-1]:
final2 = final2 + int(number) * multiple
multiple = multiple * 2
#_________________________________________________________________________________________________________
total = final1 + final2
number = total
output = str()
while number > 0:
output = str(number % 2) + output
number = int(number / 2)
print("")
print("The total of {} and {} is {}.".format(firstnumber, secondnumber, output))