There is no error in the execution of the code, however the return I am having in the excel table is that is the problem

Asked

Viewed 35 times

0

I have a problem returning my code. I do not get any error in the execution, but the problem is the return in the Excel table. I’m doing a case study of my college and the system is about customer registration and ordering. The class and object code just below:

Class


class Cliente:
    def __init__(self, nome, s_nome, tel, email, social_n, endereco, data_cad):
        self.set_nome(nome)
        self.set_s_nome(s_nome)
        self.set_tel(tel)
        self.set_email(email)
        self.set_social_n(social_n)
        self.set_endereco(endereco)
        self.set_data_cad(data_cad)

    # SETs dos dados do cliente

    def set_nome(self, nome):
        self.nome = nome

    def set_s_nome(self, s_nome):
        self.s_nome = s_nome

    def set_tel(self, tel):
        self.tel = tel

    def set_email(self, email):
        self.email = email

    def set_social_n(self, social_n):
        self.social_n = social_n
    
    def set_endereco(self, endereco):
        self.endereco = endereco

    def set_data_cad(self, data_cad):
        self.data_cad = data_cad

    # GET para retornar o "database"

    def get_database(self,rows):
        one_cliente = []
        for n in range(rows):
            one_cliente.append({
                'id': n + 1,
                'nome':self.nome,
                'sobrenome':self.s_nome,
                'telefone':self.tel,
                'email':self.email,
                'rede_social':self.social_n,
                'endereco':self.endereco,
                'data_de_cadastro':self.data_cad
            })
        return one_cliente 

another code


import os
import xlwt
import xlrd
from datetime import date
from class_cliente import Cliente

def main():
    cont = 1

    print('''\
        O que deseja fazer?
        [1] Tabela de Clientes
        [2] Tabela de Encomendas
        [3] Excluir Cadastro
        [4] Relatório Excel
        [5] Sair
        ''')

    opcGeral = int(input('Digite Aqui: '))
    while True:
        if opcGeral == 1:

            nome = input('Nome do cliente: ')
            s_nome = input('Sobrenome do cliente: ')
            tel = input('Telefone pra conta: ')
            email = input('email: ')
            social_n = input('Rede: ')
            endereco = input('Endereço: ')                
            data_cad = date.today()

            workbook = xlwt.Workbook()
            worksheet = workbook.add_sheet("cliente")

            dado_cliente = Cliente(nome, s_nome, tel, email, social_n, endereco, data_cad)
            cont_cadas = dado_cliente.get_database(cont) 
            cont += 1

        elif opcGeral == 2:
            pass

        elif opcGeral == 4:
            worksheet.write(0,0,u'ID')
            worksheet.write(0,1,u'NOME')
            worksheet.write(0,2,u'SOBRENOME')
            worksheet.write(0,3,u'TELEFONE')
            worksheet.write(0,4,u'EMAIL')
            worksheet.write(0,5,u'REDE SOCIAL')
            worksheet.write(0,6,u'ENDEREÇO')
            worksheet.write(0,7,u'DATA DO CADASTRO')

            for i, cliente_one in enumerate(cont_cadas):
                worksheet.write(i + 1, 0, label = cliente_one['id'])
                worksheet.write(i + 1, 1, label = cliente_one['nome'])
                worksheet.write(i + 1, 2, label = cliente_one['sobrenome'])
                worksheet.write(i + 1, 3, label = cliente_one['telefone'])
                worksheet.write(i + 1, 4, label = cliente_one['email'])
                worksheet.write(i + 1, 5, label = cliente_one['rede_social'])
                worksheet.write(i + 1, 6, label = cliente_one['endereco'])
                worksheet.write(i + 1, 7, label = cliente_one['data_de_cadastro'],style=xlwt.easyxf(num_format_str='dd/mm/yyyy'))

            workbook.save('cliente.xls')


        opcGeral = input('Cadastrar mais clientes: [S/N]')
        if opcGeral.lower() == 's' :
            print('''\
        O que deseja fazer?
        [1] Tabela de Clientes
        [2] Tabela de Encomendas
        [3] Excluir Cadastro
        [4] Relatório Excel
        [5] Sair
        ''')

            opcGeral = int(input('Digite Aqui: '))

        else:
            break

    
if __name__ == "__main__":
    main()

Note: The second code is not yet finished

I’m using lib xlwt to write in Excel only when I run the code when I start registering the clients becomes normal and inserts the data normally, but when I do two entries instead of writing in Excel the two people registered she ends up writing twice the last person registered... I wonder what I’m doing wrong.

  • User defined class is of the changeable type. Read this post because I believe that’s the problem.

  • How are you running the program when you notice the error? Do you register a client, save it in excel, register the second one and save it to Excell again? Or you register twice and save twice?

  • save only once register several and save once.

  • Okay, I get your problem. You realize that cont_cadas is a list that only contains the registration of a client? You are not saving the information of each client but of a single one.

1 answer

0

Your problem is that your list one_cliente that was being generated in the class Cliente contained n-times the same client, with a different id. To resolve this I suggest you use a static class variable Cliente to store each new client registered. To do this just create the empty list one_cliente out of function get_database and refer to it as Cliente.one_cliente as in the example below:

class Cliente:
    one_cliente = []

    def __init__(self, nome, s_nome, tel, email, social_n, endereco, data_cad):
        self.set_nome(nome)
        self.set_s_nome(s_nome)
        self.set_tel(tel)
        self.set_email(email)
        self.set_social_n(social_n)
        self.set_endereco(endereco)
        self.set_data_cad(data_cad)

    # SETs dos dados do cliente

    def set_nome(self, nome):
        self.nome = nome

    def set_s_nome(self, s_nome):
        self.s_nome = s_nome

    def set_tel(self, tel):
        self.tel = tel

    def set_email(self, email):
        self.email = email

    def set_social_n(self, social_n):
        self.social_n = social_n
    
    def set_endereco(self, endereco):
        self.endereco = endereco

    def set_data_cad(self, data_cad):
        self.data_cad = data_cad

    # GET para retornar o "database"

    def get_database(self, id):
        Cliente.one_cliente.append({
            'id': id,
            'nome':self.nome,
            'sobrenome':self.s_nome,
            'telefone':self.tel,
            'email':self.email,
            'rede_social':self.social_n,
            'endereco':self.endereco,
            'data_de_cadastro':self.data_cad
        })
        return Cliente.one_cliente

this way you don’t need to change but nothing in the rest of the code

Browser other questions tagged

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