print ("...") is running multiple times

0

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")    # <--- ....
    
asked by anonymous 28.01.2018 / 16:31

1 answer

1

Let's see if I can help you. Starting by dividing the doubt into two:

1 - When I type a wrong CPF and then type the valid one, the message "The Cpf ...." is printed twice [...].

  if Verifica1 >= 8: # CPF COM Padrao -> 111.111.111.11 e etc.. sao Invalidos

        print("CPF Invalido.")
        os.system("pause")
        self.GetCPF()
        sys.exit(0) <== Pelo que pesquisei, isso deve resolver

When you inform that the CPF is invalid and calls the "getCPF" function again, you do NOT STOP executing the first "CPF validation". After this getCPF, try to "stop" the execution. Why parsing the code, it is after processing the "self.getCPF ()" by continuing the execution, ie going to the "self.verifyDigit1 ()".

2 - Doubt 2 is related to the "bug" of the first. When an invalid CPF is typed, it is NOT FOR execution.

I'll try to make it clear by showing the execution below:

Consider that I entered an invalid CPF initially:

    if Verifica1 >= 8: # CPF COM Padrao -> 111.111.111.11 e etc.. sao Invalidos

        print("CPF Invalido.") <== Da o alerta
        os.system("pause") 
        self.GetCPF() <=== Pede o novo CPF !!

    self.VerificaDigito1() <== Mesmo eu digitando o CPF inválido, ele irá executar essa função depois de processar todo o "self.getCpf"
    
28.01.2018 / 17:41