Hello!
Here is the code, pls have a look fist:
class Cstudent():
def __init__(self, name):
self.name = name
self.subjets = []
self.mark = []
def add_subject(self, subject):
self.subjets.append(subject)
def add_mark_student(self, subject, mark):
subject.add_mark(mark)
def __str__(self):
return 'I am object of student: %s'% \
(self.name)
def print_list_of_subjects(self):
lst = []
for prd in self.subjets:
lst.append(str(prd))
return '\n'.join(lst)
class Csubject():
def __init__(self, name):
self.name = name
self.mark = []
def add_mark(self, mark):
self.mark.append(mark)
def average_subject(self, subject):
return sum(self.mark) / len(self.mark)
def __str__(self):
return 'I am object of subject: %s and have marks: %s' % \
(self.name, str(self.mark))
def main():
# inicalization of objekt - student:
#-------------------------------
janko = Cstudent('Janko Moor')
janka = Cstudent ('Janka Vaskova')
# inicialization of subjects:
#-------------------------------
fyz = Csubject('fyzic')
eng = Csubject('english')
mat = Csubject('matematic')
hist= Csubject('history')
bio = Csubject('biology')
# adding a subject to a particular object:
#-------------------------------
janko.add_subject(fyz)
janko.add_subject(eng)
janko.add_subject(mat)
janka.add_subject(hist)
janka.add_subject(bio)
janka.add_subject(eng)
janka.add_subject(fyz)
# adding a mark to a particular object:
#-------------------------------
janko.add_mark_student(fyz, mark=4)
janko.add_mark_student(fyz, mark=1)
janko.add_mark_student(fyz, mark=1)
janka.add_mark_student(hist,mark=1)
janka.add_mark_student(hist,mark=2)
janka.add_mark_student(hist,mark=3)
janka.add_mark_student(fyz,mark=1)
# output:
#-------------------------------
print janko
print janko.print_list_of_subjects()
print fyz.average_subject('fyzic')
print '-----------------------------------------------------'
print janka
print janka.print_list_of_subjects()
print hist.average_subject('history')
if __name__ == '__main__':
main()
output:
I am object of student: Janko Moor
I am object of subject: fyzic and have marks: [4, 1, 1, 1]
I am object of subject: english and have marks: []
I am object of subject: matematic and have marks: []
1
-----------------------------------------------------
I am object of student: Janka Vaskova
I am object of subject: history and have marks: [1, 2, 3]
I am object of subject: biology and have marks: []
I am object of subject: english and have marks: []
I am object of subject: fyzic and have marks: [4, 1, 1, 1]
--------------------------------------------------------------------------------------
I added mark 1 to student janka and subject fyz. BUT outout is [4, 1, 1, 1]
which is not what I exept. ([1] what I exept). What´s more this mark is also added to student janko, as you can see in the result. It´s not what I want.
Pls can you help?