The answer from falsetru won't work if the number starts with leading zeroes and you want to accept this as valid input.
You may want to use raw_input
instead of input
-- this way you'll get the raw string that the user enters. Then you can easily test the length of that string, in addition to converting the number with int(<x>, 2)
. Something like this, for instance:
while True:
raw = raw_input('Please enter 64 bits: ')
try:
num = int(raw,2)
except ValueError:
print("Not a valid binary number")
continue
if len(raw)==64:
print ('Accepted')
break
else:
print ('Not Accepted, need exactly 64 bits. Try again')