Well, I'm trying to learn python because it seems to me a language with some interesting applicability in the most diverse areas (and I'm interested in raspberry).
As I am learning, I like to go about something that I need to apply the knowledge.
As such I am developing a system consisting of a class dictionary (Class Created) whose identifier of each class is the acronym received.
Each class consists of an array of students (Created Class) and an acronym that identifies it.
All classes have setters and getters for all attributes.
My problem is that whenever I add a new student to a class already created and inserted into the class array, it adds it to all classes contained in the array and I can not find the focus of the problem.
The code for my class class is as follows:
class Turma:
nome = None
sigla = None
alunos = []
def __init__(self, _nome, _sigla):
self.nome = _nome
self.sigla = _sigla
def getNome(self):
return self.nome
def addAluno(self, _aluno):
self.alunos.append(_aluno)
def getSigla(self):
return self.sigla
def getAlunos(self):
return self.alunos
The code for my student class is as follows:
class Aluno:
nome = None
idade = None
numero = None
def __init__(self, _nome, _idade, _num):
self.nome = _nome
self.idade = _idade
self.numero = _num
def getNome(self):
return self.nome
def getNumero(self):
return self.numero
def getIdade(self):
return self.idade
def setNome(self, _nome):
self.nome = _nome
def setIdade(self, _idade):
self.idade = _idade
def setNome(self, _num):
self.numero = _num
In my system control file I have the following features:
turmas = {}
def createTurma():
nome = input("Insira o nome da turma: ")
sigla = input("Insira a sigla da turma: ")
turmas[sigla] = Turma(nome, sigla)
print(turmas);
print("Turma Adicionada com Sucesso!")
def listTurma():
sigla = input("Insira a sigla do curso: ")
print(turmas[sigla].getNome())
def addAlunoTurma():
num = 0
nome = input("Insira o nome do aluno: ")
idade = input("Insira a idade do aluno: ")
sigla = input("Insira a sigla da turma: ")
_aluno = Aluno(nome, idade, 0)
turmas[sigla].addAluno(_aluno)
def getAlunosByTurma():
sigla = input("Insira a sigla da turma: ")
teste = turmas[sigla].getAlunos()
print(len(teste))
for i in turmas[sigla].getAlunos():
print("Nome: " + i.getNome())
print("Numero: " + str(i.getNumero()))
print("--------------")
I detected this problem when the function getAlunosByTurma () because regardless of the acronym always prints all students.
Thank you !!