954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

print the subjects from class Student

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?

vlady
Junior Poster in Training
79 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You