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?

It's a design issue. Here is my son's school report

FRANCAIS        Mrs xxx     17,0 , 13,0 , 16,0 , 19,0 , 10,5 , 18,0
LATIN           Mrs xxx     5,0
ANGLAIS LV1     Miss xxx    12,5 , 18,0
ESPAGNOL LV1    Mrs xxx     12,5 , 8,0 , 12,0
HIST/GEO/ED.CIV Miss xxx    15,0 , 13,5
MATHEMATIQUES   Mrs xxx     16,0 , 17,5 , 20,0 , 17,5
PHYSIQUE        Miss xxx    10,0 , 13,5
SC VIE ET TERRE Mrs xxx     18,5 , 15,0

As you can see there is a list of subjects and for each subject, a list of marks. These lists of marks belong to both my son and the subject. So I suggest the following class design:

class Student:
    # members
    name
    report

class Subject:
    # members
    name
    teacher_name

"report" can be a list of pairs (subject, list of marks), or a dictionary subject --> list of marks.

You should connect the marks to student, not subject, each student have their own marks.

pls can you show me an example? (using code)

I tried something like this but doesn´t work.

class Cstudent():
    def __init__(self, name):
        self.name = name
        self.subjets = []
        self.mark = []
        self.report={}

        def test(self,subject):
             self.report[subject]=self.mark  

sefl.mark is supposed to be a list of marks => [1,2,3]
so I simly want to add to a subject for example fyz these mark
=> self.report={'fyz':[1,2,3]}
and this I like to joint to student name janko

I have a view but because I learn classes it´s quiet difficult to make it. If I have an examle, it´s better... I can progres quickly rather than spending a lot of time on it.

Here is an example

class Student(object):
    def __init__(self, name):
        self.name = name
        self.report = dict()
        
    def __repr__(self):
        return "Student({0})".format(repr(self.name))

    def add_subject(self, subject):
        self.report[subject] = list()
        
    def add_mark(self, subject, mark):
        self.report[subject].append(mark)
        
    def subjects(self):
        return self.report.keys()
        
    def s_subjects(self):
        return ", ".join(subject.name for subject in self.subjects())

    def average(self, subject):
        L = self.report[subject]
        return float(sum(L))/len(L)

class Subject(object):
    def __init__(self, name, teacher = "unknown"):
        self.name = name
        self.teacher = teacher
        

if __name__ == "__main__":
    janko = Student('Janko Moor')
    janka = Student ('Janka Vaskova')
    
    fyz = Subject('fyzic')
    eng = Subject('english')
    mat = Subject('matematic')
    hist= Subject('history')
    bio = Subject('biology')

    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)

    janko.add_mark(fyz, mark=4)
    janko.add_mark(fyz, mark=1)
    janko.add_mark(fyz, mark=1)

    janka.add_mark(hist,mark=1)
    janka.add_mark(hist,mark=2)
    janka.add_mark(hist,mark=3)
    janka.add_mark(fyz,mark=1)
    
    print janko
    print janko.s_subjects()
    print janko.average(fyz)

    print janka
    print janka.s_subjects()
    print janka.average(hist)

""" my output -->
Student('Janko Moor')
fyzic, english, matematic
2.0
Student('Janka Vaskova')
biology, fyzic, history, english
2.0
"""

hello

Thank you for the example! I don´t understand this method you wrote, pls can you explain it for better understanding? (if you can make a simple equivalent it would be great)
I appreciate!

def s_subjects(self):
        return ", ".join(subject.name for subject in self.subjects())
def __repr__(self):
        return "Student({0})".format(repr(self.name))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.