0
I’m doing this show:
Develop a program that requires typing of a CPF number in format
xxx.xxx.xxx-xx
and indicate if it is a valid or invalid number via validation of check digits and formatting characters.
The code is this:
pf = input("CPF(xxx.xxx.xxx-xx) :") #3 7 11
for letra in cpf:
if(cpf[3] !=".") or (cpf[7] !=".") or (cpf[11] !="-"):
cpf = input("O 'CPF' pricisa estar no formato (xxx.xxx.xxx-xx) :")
else:
print("O 'CPF' está no formato correto")
But when I say run, this is the way out:
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
O 'CPF' está no formato correto
How can I just go out once?
That way, the system would request the CPF again only once, try to exchange the if for while
– Nathan Schroeder
Actually the CPF check goes beyond that. The last two digits are the check digits and it is necessary to validate them.
– Woss
@Nathanschroeder I understand, yes, it makes more sense then to be a
while
until validation passes. @Andersoncarloswoss I am not familiar with CPF, but I understand the validation that is required. I just used the original code example.– Leite
In fact, for your solution the input
...........-..
would be a valid CPF: https://ideone.com/JktKch– Woss
True! I didn’t even confirm that the code was validating correctly, I just noticed that the cause of the repetition was the cycle for each letter, but then it wasn’t even used correctly. Despite everything, I voted for the answer with the most appropriate solution, this one I don’t know why has an upvote.
– Leite