0
I’m trying to make a program in python with two classes: Discipline and Room, at a certain point I want to relate the room registered in the class Room in the class Discipline, the code does not generate error, but at the end the room is not linked
class Sala:
def __init__(self, numero):
self.numero = numero
self.andar = self.setAndar(numero)
self.capacidade = None
def setAndar(self, numero):
self.andar = numero[0]
return self.andar
def set_capacidade(self, capacidade):
self.capacidade = capacidade
def get_sala(self):
return self.numero
def set_sala(self, sala):
self.numero = sala
def __str__(self):
return "Sala: "+self.numero+" Andar: "+str(self.andar)+" Capacidade: "+self.capacidade
class Disciplina:
def __init__(self, nome, semestre):
self.nome = nome
self.semestre = semestre
self.sala = None
def set_sala(self, sala):
self.sala = sala
def get_sala(self):
if self.sala is None:
return "Não definida!"
if self.sala is not None:
return self.sala
def __str__(self):
return "Disciplina: "+self.nome+" Semestre: "+self.semestre+" Sala : "+str(self.get_sala())
def relacionar_salas():
imprime_disciplina()
escolha_disciplina = input("Qual a disciplina gostaria de vincular a sala? ")
objeto_discplina = classeDisciplina.Disciplina(escolha_disciplina, None)
relatorio_salas()
escolha_sala = input("A qual Sala? ")
objeto_sala = classeDisciplina.Disciplina(escolha_sala, None)
if escolha_disciplina not in lst_disciplina:
print("Isso nao existe")
else:
objeto_discplina.set_sala(objeto_sala)
Why the
objeto_sala
is an instance of the classDisciplina
?– Woss
A good answer to this question would have to go through a lot of concepts - it would have to be practically a (well-given) class of object orientation use. One place where perhaps the mix of "how to create related objects" is well explained can be the tutorial frameworks like Django, which map directly to related tables. I suggest you read the Django tutorial - the "models" part-even if you don’t want to do your web application right now. https://django-portuguese.readthedocs.io/en/1.0/intro/tutorial01.html - -
– jsbueno
But warning - you are with more basic programming questions, from the example of the code you gave - practiment and writing random calls to functions, etc... may have to do more basic exercises before progressing to where you want to go.
– jsbueno
Worth a look at article, must give the solution.
– MedroPelo