1.11M Members

list of objects

 
0
 

Hello!

I don´t understand how to use a condition " if i == predmet: " i is an objet type of subject

In my code I need to reach the condition:

def add_mark(self,predmet,mark):
        for i in self.predmety:
            if i == predmet:
                print True

but I don´t know how to make this condition true => "if i == predmet"
"i" is an object and it will never match this condition.
I need to get like:
if fyzika == fyzika:
print True

but i is always object and I don´t know how to chanhe it to non object. Can you help?

here is the rest of the code:

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

    def add_subject(self, predmet):
        self.predmety.append(predmet)

    def add_mark(self,predmet,mark):
        for i in self.predmety:
            if i == predmet:
                print True
                #predmet.add_znamka(mark)

    def __str__(self):
        return self.name
        #return 'som objekt studenta: ' + (self.name)
##        return 'som object studenta: %s'% \
##            (self.name)

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


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 self.nazov
##        return 'som object predmetu: %s a mam znamky: %s' % \
##            (self.nazov, str(self.znamky))

def main():

#-------------------------------
    janko = Cstudent('Janko Mrkvicka')
    janka = Cstudent ('Janka Vaskova')

#-------------------------------
    janko.add_subject(Cpredmet('fyzika'))
    janko.add_subject(Cpredmet('english'))

    janka.add_subject(Cpredmet('dejepis'))
    janka.add_subject(Cpredmet('fyzika'))

#-------------------------------
    janko.add_mark(predmet='fyzika',mark=4)
    janko.add_mark(predmet='fyzika',mark=1)
    janko.add_mark(predmet='english',mark=1)

    janka.add_mark(predmet='dejepis',mark=2)
    janka.add_mark(predmet='dejepis',mark=3)
    janka.add_mark(predmet='fyzika',mark=1)

# output:
#-------------------------------
    print janko
    print janko.print_zoznam_predmetov()
    print '-----------------------------------------------------'
    print janka
    print janka.print_zoznam_predmetov()
    
if __name__ == '__main__':
    main()
 
1
 

I do not think I understand what is all this fuss but maybe something like:

def add_mark(self, predmet,mark):
        for p in self.predmety:
            #print p.nazov
            if p.nazov == predmet:
                p.add_znamka(mark)
                break
        else:
            raise ValueError("%s not taking course %s" % (self.name, predmet))
 
0
 

I do not think I understand what is all this fuss but maybe something like:

def add_mark(self, predmet,mark):
        for p in self.predmety:
            #print p.nazov
            if p.nazov == predmet:
                p.add_znamka(mark)
                break
        else:
            raise ValueError("%s not taking course %s" % (self.name, predmet))

super! thank you!

Question Answered as of 2 Years Ago by pyTony
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article