Hello Guys!

Can anybody help me with this code? So what I need?
here is it:
I want to fill out global variable "student_marks" with a grade for each student and keep it once is loaded. So it means without calling the function "def add_mark()" I will be able to print just global variable "student_marks" with its all students and marks.
I can do it in interpreter but not in script code. Pls, I need your help.

I approciate it, many thanks!

student_list=['juraj','kristina','peto','alica','vlado','natalia','roman','silvia']
student_marks = {}


def add_student(student):
    student_list.append(student)

def remove_student(student):
    student_list.remove(student)

def marks_student(student_list):
    for student in student_list:
        student_marks[student]=[]
    return student_marks

def add_mark():
    global student_marks
    number_students=len(student_list)
    for n in range(number_students):
        student_marks[raw_input('student')].append(raw_input('mark'))
    return student_marks



def main():
    marks_student(student_list)
    #print student_marks
    add_mark()
    print student_marks1
##    add_mark('juraj','A')
##    add_mark('kristina','B')
##    add_mark('peto','A')
##    add_mark('alica','C')
##    add_mark('vlado','A')
##    add_mark('natalia','B')
##    add_mark('roman','A')
##    add_mark('silvia','C')
    #print student_marks

if __name__ == '__main__':
    main()

Recommended Answers

All 4 Replies

You can just load a dict() & print it.

foo = {"alice" : "A+", "smith" : "A", "daemon" : "B+", "lily" : "B" }
print foo

You can also capture the data interactively with your function add_mark(), then save the dictionary to a file and read the dictionary from this file the next time you run your program

from cPickle import dump, load
marks = {"alice" : "A+", "smith" : "A", "daemon" : "B+", "lily" : "B" }

# save dict to file
with open("marks.pkl", "wb") as fout:
    dump(marks, fout)
# read dict from saved file
with open("marks.pkl", "rb") as fin:
    foo = load(fin)
print foo # prints {'smith': 'A', 'daemon': 'B+', 'lily': 'B', 'alice': 'A+'}

If you want to save more than one grade per student, use a dictionary of lists.

marks = {"alice" : ["A+", "B-"], "smith" : ["A", "A"], "daemon" : ["B+", "B"], "lily" : ["B", "C"] }

thank you. I try it trough the module pickle as Gribouillis has shown in his example...
Friend of mine also told me to try it using pickle but I thought there is simple way... It might be, I don´t know but pickle works, so I can get by with it.

again thanks!

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.