Syntaxerror: Unexpected EOF while Parsing

Asked

Viewed 5,701 times

-1

My program is giving "Syntaxerror: Unexpected EOF while Parsing", my teacher said it would be by not closing the while, but I can not, I tried several ways, still giving error

class Conta:

    def __init__(self,cod,nome,saldo):
        self.cod = cod
        self.nome = nome
        self.saldo = saldo

    def getNome(self):
        return self.nome

    def setNome(self,Nome):
        self.nome = Nome

    def getCod(self):
        return self.cod

    def setCod(self,Cod):
        self.cod = Cod

    def getSaldo(self):
        return self.saldo

    def setSaldo(self,Saldo):
        self.saldo = Saldo

    def deposito(self):
        novaQuantia = float(input('Informe o valor do depósito:\n'))
        self.saldo = self.saldo + novaQuantia

    def saque(self):
        saque = int(input('Informe o valor do saque:\n'))
        if saque <= self.saldo:
            self.saldo = self.saldo - saque
            return True
        else:
            print('Saldo insuficiente!')
            return False

if __name__ == "__main__":
        a = 0
        codigo = []
        nome = []
        saldo = []
        while a != 7:
            print('1 - Inserir\n')
            print('2 - Excluir\n')
            print('3 - Alterar\n')
            print('4 - Consultar\n')
            print('5 - Operação\n')
            print('6 - Listar\n')
            print('7 - Sair\n')
            try:
                a = int(input())
                if len(codigo)!=len(nome) and len(nome)!=len(saldo):
                            raise Exception("Informação incompleta!")

                if a == 1:
                    try:
                        codigo = int(input('Informe o código da conta: \n'))
                        nome = str(input('Informe o nome do titular: \n'))
                        saldo = float(input('Informe o saldo da conta: \n'))
                        codigo.append(codigo)
                        nome.append(nome)
                        saldo.append(saldo)
                    except ValueError:
                        print("Valor inválido!")

                if a == 2:
                    try:
                        p = int(input('Informe a posição a ser removida:\n'))
                        codigo.pop(p)
                        nome.pop(p)
                        saldo.pop(p)
                    except IndexError:
                        print("Index inválido!")

                if a == 3:
                    try:
                        codigo = int(input('Informe o código da conta: \n'))
                        saldo = int(input('Informe o novo saldo da conta: \n'))
                        saldo.pop(codigo.index(codigo))
                        saldo.insert(codigo.index(codigo),saldo)
                    except ValueError:
                        print("Código inválido!")  

                if a == 4:
                    try:
                        codigo = int(input('Informe o código da conta a ser consultado: \n'))
                        print("Código: {} | Nome: {} | Saldo: {}".format(codigo.index(codigo),saldo.index(saldo),nome.index(nome)))
                    except ValueError:
                        print("Código inválido!") 

                if a == 5:
                    try:
                        b = int(input('Informe a operação a ser feita:\n'))
                        print('1 - Realizar saque\n')
                        print('2 - Realizar depósito\n')
                        if b == 1:
                            self.saque()
                        else:
                            self.deposito()
                    except ValueError:
                        print("Código inválido!") 

                if a == 6:
                    try:
                        for codigo, nome, saldo in zip(codigo, nome, saldo):
                         print("Código: {} | Nome: {} | Saldo: {}".format(codigo, nome, saldo))    
                    except ValueError:
                        print("Código inválido!") 

                if a == 7:
                    print("Operação finalizada!")
  • 1

    Is missing the except of try within the while, that’s all.

1 answer

0

As Giovanni said, we’re missing one try with except.

Moreover, in this particular case, only one try:

while a != 7:
           try:
                if len(codigo)!=len(nome) and len(nome)!=len(saldo):
                            raise Exception("Informação incompleta!")
                ...
                if a == 7:
                    print("Operação finalizada!")
           except ValueError as e:
                print(e)
           except IndexError as e:
                print(e)
           except Exception as e:
                print(e)

If any ValueError within this try, he will play for the except; no exceptions (joke intended).
And there’s no need to use a try nestled to another try, because that statement serves for error handling, and it would be redundant to treat one error within another error.

  • 1

    The question of needing only one try is quite questionable and almost always goes against the idea of exception, as it is usually interesting to treat a specific exception of a limited block of code.

  • He said more in the case of his specific code, since there’s only ValueError and a generic exception raise Exception, but try to be more direct.

Browser other questions tagged

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