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

Asked

Viewed 191 times

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")    # <--- ....

1 answer

1

Let’s see if I can help you. Starting by splitting the doubt in two:

1 - When I type a wrong CPF and then type the valid one, the "Cpf..." message 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 report that the CPF is invalid and call the "getCPF" function again, you do NOT STOP executing the first "CPF validation". After this getCPF, try to "stop" the execution. Why analyzing the code, it is after processing the "self.getCPF()" continuing the execution, ie going to the "verified self.Igito1()".

2 - Doubt 2 is related to the "bug" of the first one. When typing an invalid CPF, it does not STOP the execution.

I will try to make it clearer by showing the execution below:

Consider that I typed 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"
  • Vlw =) I put an Else after each solved If

  • Good Vitor!! I’m happy to help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.