Hello,
I am learning classes, and find it a bit difficult... :-(

Mine intention is to add subject(predmet) and mark to the objet janko but
if I search in self.predmety, I get False, means that it can't
recognise predmet in self.predmety list
State of self.predmety is as folloving:
[<__main__.Cpredmet instance at 0x010F5260>, <__main__.Cpredmet instance at 0x010F5210>, <__main__.Cpredmet instance at 0x010F5238>]

I presume that it can not recognize predmet (subject) in that state... Pls I need advice how to find a predmet in self.predmety list.
I approciate your help!

class Cstudent():
    def __init__(self, name):
        self.name = name
        self.predmety = []
        self.mark = []
        self.absence = 0

    def add_subject(self, predmet):
        '''

        @param predmet: objekt typu predmet
        '''
        self.predmety.append(predmet)

    def add_mark(self, predmet, mark):
        if predmet in self.predmety:
            print True
        else:
            print False
   
    def print_zoznam_predmetov(self):
        return self.predmety
   
class Cpredmet():
    def __init__(self, nazov):
        self.nazov = nazov
        self.znamky = []

    def add_znamka(self, znamka):
        self.znamky.append(znamka)

    def average_predmet(self, predmet):
        return sum(self.znamky) / len(self.znamky)

    
def main():

    janko = Cstudent('janko mrkvicka')

    fyz = Cpredmet('fyzika')
    eng = Cpredmet('english')

    janko.add_subject(Cpredmet('matematika'))
    janko.add_subject(fyz)
    janko.add_subject(eng)

    janko.add_mark(predmet='fyzika', mark=4)

    print janko.print_zoznam_predmetov()


if __name__ == '__main__':
    main()

The problem is that you're passing a string 'fyzika' to add_mark() instead of a Cpredmet object. I think it would be easier if self.predmety was a dictionary name --> Cpredmet instance with that name . For example you would write

def add_subject(self, predmet):
        '''

        @param predmet: objekt typu predmet
        '''
        self.predmety[predmet.nazov] = predmet

and then

if 'fyzika' in self.predmety:

would work.

ok, but I meant it differently..

class Cstudent():
    def __init__(self, name):
        self.name = name
        self.predmety = []
        self.mark = []
        self.absence = 0

    def add_subject(self, predmet):
        '''

        @param predmet: objekt typu predmet
        '''
        self.predmety.append(predmet)

    def add_mark(self, predmet, mark):
        if predmet in self.predmety:
            print True
        else:
            print False
        # TODO: dokoncit!
        #- prehladat vsetky predmety
        #  - a tomu ktory ma rovnaky nazov zavolat metodu add_znamka


    def print_zoznam_predmetov(self):
        lst = []
        for prd in self.predmety:
            lst.append(str(prd))
        return '\n'.join(lst)
        #return self.predmety

class Cpredmet():
    def __init__(self, nazov):
        self.nazov = nazov
        self.znamky = []

    def add_znamka(self, znamka):
        self.znamky.append(znamka)

    def average_predmet(self, predmet):
        return sum(self.znamky) / len(self.znamky)

    def __str__(self):
        return 'som object predmet %s  a mam znamky: %s' % \
            (self.nazov, str(self.znamky))


def main():

    janko = Cstudent('janko mrkvicka')

    fyz = Cpredmet('fyzika')
    eng = Cpredmet('english')

    janko.add_subject(Cpredmet('matematika'))
    janko.add_subject(fyz)
    janko.add_subject(eng)

    janko.add_mark(predmet='fyzika', mark=4)

    print janko.print_zoznam_predmetov()


if __name__ == '__main__':
    main()

here is output>

False
som object predmet matematika  a mam znamky: []
som object predmet fyzika  a mam znamky: []
som object predmet english  a mam znamky: []

hello guys!

So I did something but I found out that is messy a bit. Look at the code:

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]
2

I want to add a mark for a janka and the subject fyz (mark=1) but what happen is that it add all marks from fyz to janka + 1 so output is [4,1,1,1] but also mark 1 is addet to object janko!
I don't know what is going on here....

Pls help!

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.