0
I made a program to check if the reported CPF is valid, but a bug is happening:
When I type a valid CPF immediately, the "Cpf..." message is printed once.
When I type a wrong number and then enter the valid one, the message "The number...." is printed twice, and so on: the number of times that an invalid CPF is passed is the number of extra times that is printed the "The CPF: ....".
I can’t understand why that is.
Another thing: when I put one print()
within a while
, the print
is executed more times than the while
: the while
rotates 3 times and the print
is displayed 6 times.
Does anyone know how to fix 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") # <--- ....
Vlw =) I put an Else after each solved If
– Vitor de Jesus
Good Vitor!! I’m happy to help you.
– Renan Porto