I’m not being able to assign value to my attribute by my method

Asked

Viewed 35 times

-1

class Aluno:
    nome = ""
    ra = int
    nota_ac = []
    nota_prova = float
    faltas = int
    media = float
    aprovado = bool

    def __init__(self, arquivo):
        self.arquivo = open(arquivo, 'r')
        linha = self.arquivo.read()
        palavra = linha.split("\n")
        self.nome = palavra[0]
        self.ra = palavra[1]
        for x in palavra[2:6+1:1]:
            self.nota_ac.append(float(x))
        self.nota_prova = float(palavra[7])
        self.faltas = int(palavra[8])
        self.media = 0.0
        self.aprovado = False
        self.arquivo.close

    def calcular_aprovacao(self):
        self.media = ((sum(self.nota_ac)/5) + (self.nota_prova))/2
        if self.media >= 6.0 and int(self.faltas) <= 20:
            self.aprovado = True
            return self.aprovado
        else:
            self.aprovado = False
            return self.aprovado

    def mostrar_nota(self):
        self.media = ((sum(self.nota_ac)/5) + (self.nota_prova))/2
        return self.media

    def escrever_situacao(self, nome_arquivo):
        arq = open(nome_arquivo, 'w')
        freq = ((80 - self.faltas)/80)*100
        arq.write(self.ra+':'+self.nome+"\n")
        arq.write(str("%.2f" % Aluno.mostrar_nota(self) + "\n"))
        arq.write(str("%.1f" % freq)+"%"+"\n")
        if Aluno.calcular_aprovacao(self) is True:
            arq.write("Aprovado")
        else:
            arq.write("Reprovado")

Good Galera I made this code that simply takes the values of a file . txt does some calculations and creates another txt file with the media and whether it was approved or not. My only problem is that I am unable to assign the media account in my self.media attribute, I would like to know why, and I would also like to know if my code is more than I should. It’s been almost 1 year that I’m studying python.

  • Clarify better what you intended to happen and what is actually happening

1 answer

0


So whenever you can try to provide as much information as possible to facilitate understanding of who is trying to help, okay?

Having said that, I took a look at your code and put it to run here, it worked normally, at least for what I understood from the error. But continuing on your code I’ll make a few remarks:

Within the Student class you prompted your attributes with references to the classes they represent leaving the code a little polluted:

class Aluno:
    nome = ""
    ra = int
    nota_ac = []
    nota_prova = float
    faltas = int
    media = float
    aprovado = bool

This is an unnecessary factor in my opinion since python is a dynamic typing language, IE, it is responsible for this role during its execution and you do not need to worry, moreover, within your constructor(init) you created an instance attribute filing cabinet unnecessary and still performed some operations, all these factors complicate much the understanding and does not contribute to you have a clean code.

The alternative would be for you to individualize the file opening and analyzing operation within a method, and since this method will not be linked to the Student object but the class, it can be a staticmethod, could be a classmethod but as we do not intend to modify the class status, nor access any of its attributes we will choose to staticmethod

Example:

class Aluno:
    def __init__(self,arquivo,aprovado=False):
        palavras = Aluno.cria_aluno(arquivo)
        self.nome = palavras[0]
        self.ra = palavras[1]
        self.notas_ac = [float(nota) for nota in palavras[2:7]]
        self.nota_prova = float(palavras[7])
        self.faltas = int(palavras[8])
        self.set_media()
        self.aprovado = aprovado

    @staticmethod
    def cria_aluno(arquivo):
        arq = open(arquivo,'r')
        palavras = arq.read().split("\n")
        arq.close()
        return palavras

Note that to define the media I used a method that already assigns the average within the constructor itself.

def set_media(self):
   self.media = ((sum(self.notas_ac)/len(self.notas_ac)) + (self.nota_prova))/2

About your method of calculating approval, note that you need to call the Return twice, it would make it easier if you just call it once, at the end of the method returning itself self approved.

def calcular_aprovacao(self):
        if self.media>=4 and self.faltas<=20:
            self.aprovado=True
        else:
            self.aprovado=False

        return self.aprovado

Note that I no longer need to calculate the average every time I want to calculate the approval, because I already did this during the creation of the student instance.

I hope I’ve helped!

  • I even apologize for the post, I did not explain exactly what I wanted. I would like to thank you for your post, it cleared all my doubts regarding the attributes that were before the init, to parts of the methods as well. Regarding Static method, I only used it a few times in abstract classes, so I didn’t use it earlier, I didn’t know it was so versatile. Thank you in advance.

Browser other questions tagged

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