-2

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))
1
  • Thanks for the reply. I looked at that page before i posted this and decided it wasnt what i was looking for. Anyway im now using regular expressions and so far its working ok. Thanks :) Commented Mar 23, 2014 at 10:17

1 Answer 1

1

Just in case anyone else needs help with this, this is how i solved my problem: http://docs.python.org/2/library/re.html

import re
valid = 0

while valid == 0:
    valid = 1
    firstnumber = input("Please enter firstnumber... ")
    if not re.match("^[0-1]{1,8}$", firstnumber):
        valid = 0
        print("Error! This program only accepts up to 8 0s and 1s!")
Sign up to request clarification or add additional context in comments.

1 Comment

You can accept your own question then. As an addition, you could also use pythonic construction if not all(ch in '01' for ch in firstnumber.strip(): or use: try: ; int(firstnumber, base=2); valid = 1;except ValueError:; valid = 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.