dimanche 18 juillet 2021

"def" bringing the wrong answer

I made a class with some functions: But when I instantiate the values the answer is bringing me 'alimentos', but that's wrong it should be 'portugues' .

I have two dictionaries and this class:

professores_x = {
    'alimentos': [{"prof_id":"xx_alimento_1", "prof_disc":"alimentos"},
                  {"prof_id":"xx_alimento_2", "prof_disc":"alimentos"}],
    'português': [{"prof_id":"xx_port_1", "prof_disc":"português"},
                  {"prof_id":"xx_port_2", "prof_disc":"português"}]}

courses_x = {'alimentos': [{"course_name":"padeiro_confeiteiro"},
                           {"course_name":"padeiro_confeiteiro"}]}


# trying refactoring

class Disciplinas_cursos_1:
    "Define the disciplinas and professors"

    def __init__(self,cursos_,professores_):
        self.cursos_ = cursos_
        self.professores_ = professores_
        for self.p in self.cursos_.keys():
            if self.p == 'alimentos': self.alimentos()
            elif self.p == 'português': self.portugues()

    def alimentos(self):
        profiel_prof_disc = self.professores_[self.p][::]
        prof_disc_al = self.p
        discipl_alimentos = [self.p,[x['prof_id'] for x in profiel_prof_disc
                                                   if x['prof_disc'] == prof_disc_al]]
        return discipl_alimentos

    def portugues(self):
        print("Now its portuguese turn")
        profiel_prof_disc = self.professores_[self.p][::]
        prof_disc_port = self.p
        print(f"see I'm printing {prof_disc_port}. It's that the same of portuguese? If' not it's wrong")
        discipl_port =[self.p,[x['prof_id'] for x in profiel_prof_disc if x['prof_disc'] ==prof_disc_port]]
        print(f"see I'm printing {prof_disc_port} and {discipl_port}")
        return discipl_port


# ok!! Now I do the instance:
disc_a = Disciplinas_cursos_1(courses_x, professores_x)
disc_a.alimentos()

Output

['alimentos', ['xx_alimento_1', 'xx_alimento_2']]

Nice, that is what I want but when I try the second function it's bring me 'alimentos' but I need 'portugues' and not 'alimentos'.

disc_a.portugues()

Output

Now its portuguese turn
see I'm printing alimentos. It's that the same of portuguese? If' not it's wrong
see I'm printing alimentos and ['alimentos', ['xx_alimento_1', 'xx_alimento_2']]

Aucun commentaire:

Enregistrer un commentaire