def somentenumeros(entrada):
try:
int(entrada)
return True
except:
return False
while True:
cpf = input('Digite seu CPF ou digite os nove primeiros digitos ')
if not somentenumeros(cpf) or (len(cpf)!= 9 and len(cpf) != 11):
print('Erro. O valor digitado era inválido.')
else:
break
print(cpf)
#http://pt.stackoverflow.com/q/187869/101
ZGVmIHNvbWVudGVudW1lcm9zKGVudHJhZGEpOgogICAgdHJ5OgogICAgICAgIGludChlbnRyYWRhKQogICAgICAgIHJldHVybiBUcnVlCiAgICBleGNlcHQ6CiAgICAgICAgcmV0dXJuIEZhbHNlCgp3aGlsZSBUcnVlOgogICAgY3BmID0gaW5wdXQoJ0RpZ2l0ZSBzZXUgQ1BGIG91IGRpZ2l0ZSBvcyBub3ZlIHByaW1laXJvcyBkaWdpdG9zICcpCiAgICBpZiBub3Qgc29tZW50ZW51bWVyb3MoY3BmKSBvciAobGVuKGNwZikhPSA5IGFuZCBsZW4oY3BmKSAhPSAxMSk6CiAgICAgICAgcHJpbnQoJ0Vycm8uIE8gdmFsb3IgZGlnaXRhZG8gZXJhIGludsOhbGlkby4nKQogICAgZWxzZToKICAgICAgICBicmVhawpwcmludChjcGYpCgojaHR0cDovL3B0LnN0YWNrb3ZlcmZsb3cuY29tL3EvMTg3ODY5LzEwMQ==