Hello,

pls can anybody show how works printing? here is the code:

class Student(object):
    def __init__(self, name):
        self.name = name
        self.report = dict()


    def add_subject(self, subject):
        self.report[subject] = list()


    def subjects(self):
        return self.report.keys()


    def s_subjects(self):
        lpre=[]
        for subject in self.subjects():
            lpre.append(str(subject))
        print lpre


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

def 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)


    print janko.s_subjects()

if __name__ == '__main__':
    main()

I just need to know the principe how to print the subjects of the student (janko). I am a bit struggling because it always shows me "objects" of all subjects. I need to see it like: matematic, fyzic,...etc. I really donĀ“t know what I need to add to the code to make it appropriate. Pls can you show me?

Replace line 18 with lpre.append(subject.name) . You can also replace line 19 with return ", ".join(lpre) .

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.