#Conversion from binary. #The input is a string of 0's and 1's. #The return value is either an #integer (the number represented #by the binary string) or "ERROR" def convert_from_binary(s): val=0 err=0 for bit in s: if bit=='0': val=2*val elif bit =='1': val=2*val+1 else: err=1 if err: return "ERROR" else: return val #A simple test of the program def test_convert(): s=raw_input("Enter string in binary") result=convert_from_binary(s) if result=="ERROR": msg="The input was not a valid binary number." else: msg="The input is the binary representation of "+str(result) showInformation(msg) #This allows us to convert a bunch of strings, until we're tired. def better_test_convert(): s=raw_input("Enter string in binary, or 'QUIT' to quit") result=convert_from_binary(s) while result!="ERROR": msg="The input is the binary representation of "+str(result) showInformation(msg) s=raw_input("Enter string in binary, or 'QUIT' to quit") result=convert_from_binary(s) print len(result) if s=="QUIT": showInformation("Goodbye") else: showInformation("Illegal input. Program terminating.")