Problem involving classes, vectors and tuples

Asked

Viewed 87 times

-2

Hi, I’m a little confused about the concept of lists and tuples in Python. In my research I noticed that tuple is a "kind of immutable list", but, despite the definition, I still can not differentiate properly at the time of developing my program. I’m stuck on the following question:

  • Build a class called Class that will represent a class of students. This class will have as attributes an array of strings containing student names and an array of floats containing each student’s grades. The class must have a constructor that will receive the two arrays as parameters, so that they can be initialized. A class should also have a method called calculator calculate and return average student grades and a print method print on the screen the list of students' names with their respective grades.

For that part, I thought of the following resolution:

class Turma:
    nome = []
    notas = []
    def __init__(self,nome,notas):
        self.nome = nome
        self.notas = notas
    def calcularMediaNota(self):
        media = sum(notas)/len(nome)
        return media
    def imprimirAlunos(self):
        relacao = zip(nome,notas)
        relacaoSet = set(relacao)
        for i in range(0,len(nome)):
            print(relacaoSet)

The question continues to request:

  • Make a class called Turmademo that must have a main method. Within the main method, a new object of the class Class must be instantiated. The constructor of the class Class should receive an array of strings containing 10 student names and an array of floats containing 10 notes, one for each student. Then to instantiate the object, execute the methods compute and print from the instantiated object.

In this second part I tried to create a resolution using test attributes, however, I believe that the model I developed so far presents errors that prevent me from reaching the final resolution.

class TurmaDemo:
    def main(self):
        turma_dados = (
            ('Teste_1',10)
            ('Teste_2',9)
            ('Teste_3',8)
            ('Teste_4',7)
            ('Teste_5',6)
            ('Teste_6',5)
            ('Teste_7',4)
            ('Teste_8',3)
            ('Teste_9',2)
            ('Teste_10',1)
        )
        turma = [Turma(*i) for i in turma_dados]
        print('A média da turma é: {}'.format(Turma.calcularMediaNota(turma)))
        Turma.imprimirAlunos()
TypeError: 'tuple' object is not callable

My level in Python and POO language is not yet well developed

If you can help me find a north I can follow or indicate the mistakes I’ve been making, it would be of great help.

2 answers

2

Syntax

You clearly did not execute your own code. If you had executed it, you would have seen that you forgot to use self when accessing class attributes. The code generates an error when calling methods calcularMediaNota or imprimirAlunos.

Validations

  • It makes no sense to accept empty lists, otherwise a division will be made by zero when calculating the average.
  • It makes no sense to accept lists of different sizes, otherwise the average will be miscalculated.

Suggested code to attack the above problems:

class Turma:
    def __init__(self, nomes, notas):
        if len(notas) == 0:
            raise Exception('notas não pode estar vazio')

        if len(notas) != len(nomes):
            raise Exception('notas e nomes devem ter o mesmo tamanho')

        self.nomes = nomes
        self.notas = notas

    def calcularMediaNota(self):
        media = sum(self.notas) / len(self.notas)

        return media

Logic - part 1

The method imprimirAlunos print the list of students and their grades several times. This makes no sense. The method should print once each student and their respective grade.

Suggested implementation:

def imprimirAlunos(self):
    for aluno, nota in zip(self.nomes, self.notas):
        print(aluno, 'tirou', nota)

Logic - part 2

In class TurmaDemo You look like you tried to invent fashion and used things you haven’t mastered yet. It was enough to have made a simpler code, following exactly what was requested by the exercise. For example:

class TurmaDemo:
    def main(self):
        nomes = [
            'Aluno A', 'Aluno B', 'Aluno C', 'Aluno D', 'Aluno E',
            'Aluno F', 'Aluno G', 'Aluno H', 'Aluno I', 'Aluno J'
        ]

        notas = [
            8, 9, 7, 9, 4,
            6, 8, 4, 5, 10
        ]

        turma = Turma(nomes, notas)

        print('Média', turma.calcularMediaNota())

        turma.imprimirAlunos()

0

I believe the first problem you’re having is how to use the first class created, Turma it receives two arrays, but you are passing from unitary values, I believe it should be instaciated this way

alunos = ('Andre', 'Bernardo', 'Carlos', 'Denilson', 'Edgar')
notas = (9, 8, 7, 9, 8)
turma = Turma(alunos,notas)

and you can also go ahead and check if the methods are working as you should

turma.calcularMediaNota()
turma.imprimirAlunos()

I suggest that first check if the class Class is working accordingly

Browser other questions tagged

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