I made a program to verify that the CPF entered is valid, but a bug is occurring:
When I type a valid CPF immediately, the message "The Cpf ...." is printed once.
When I type a wrong CPF and then type the valid one, the message "The Cpf ...." is printed twice, and so on: the number of times an invalid CPF is passed is the number of extra times which is printed on "The CPF: ....".
I can not understand why.
Another thing: When I put a print()
inside a while
, print
is executed more times than while
: while
rotates 3 times and print
is displayed 6 times .
Does anyone know how to resolve this bug?
The code:
import os # Importando o Modulo Os -> Operating Systems
class ValidaCPF:
CPF = "Vazio"
def GetCPF(self):
os.system("cls")
print("--------Regras------- ")
print("| Apenas 11 Digitos | ")
print("| Apenas -- Numeros | ")
print("|___________________| ")
print("\n")
self.CPF = input("Digite O CPF: ")
self.Test1()
def Test1(self):
os.system("cls")
if self.CPF.isdigit() != 1 or len(self.CPF) != 11: # ! O CPF INFORMATO CONTEM 11 DIGITOS?
print("CPF Invalido.")
os.system("pause")
self.GetCPF()
self.Test2()
def Test2(self):
Verifica1 = 0
Contador1 = 0
Contador2 = 1
while Contador1 < 9:
if self.CPF[Contador1] == self.CPF[Contador2]:
Verifica1 += 1
Contador1 += 1
Contador2 += 1
if Verifica1 >= 8: # CPF COM Padrao -> 111.111.111.11 e etc.. sao Invalidos
print("CPF Invalido.")
os.system("pause")
self.GetCPF()
self.VerificaDigito1()
def VerificaDigito1(self):
Test = 0
Contador1 = 0
Contador2 = 10
while 1:
Test = Test + (int(self.CPF[Contador1]) * Contador2)
Contador1 += 1
Contador2 -= 1
if Contador2 == 1:
break
Test = Test * 10
Test = Test % 11
if Test == 10:
Test = 0
if Test != int(self.CPF[9]):
print("CPF Invalido.")
os.system("pause")
self.GetCPF()
self.VerificaDigito2()
def VerificaDigito2(self):
Test = 0
Contador1 = 0
Contador2 = 11
while 1:
Test = Test + (int(self.CPF[Contador1]) * Contador2)
Contador1 += 1
Contador2 -= 1
if Contador2 == 1:
break
Test = Test * 10
Test = Test % 11
if Test != int(self.CPF[10]):
print("CPF Invalido.")
os.system("pause")
self.GetCPF()
print("O CPF: ", self.CPF, "E Valido.")
Sistema = ValidaCPF() # <--- Error de indentação A partir daqui
Sistema.GetCPF() # <--- Retirar a Tabulação
os.system("pause") # <--- ....