views:

263

answers:

2

Hi, I am incredibly new to Python and I really need to be able to work this out. I want to be asking the user via raw_input what the module and grade is and then putting this into the dictionary already defined in the Student class as grades. I've got no idea what to do! Thanks in advance!

students = [] # List containing all student objects (the database)

def printMenu(): 
    print "------Main Menu------\n" "1. Add a student\n" "2. Print all students\n" "3. Remove a student\n" "---------------------\n"


class Student:
    firstName = ""
    lastName = ""
    age = 0
    studentID = ""
    degree = ""
    grades = {"Module Name":"","Grade":""}

    def setFirstName(self, firstName):
        self.firstName = firstName

    def getFirstName(self):
        return self.firstName

    def setLastName(self, lastName):
        self.lastName = lastName

    def getLastName(self):
        return self.lastName

    def setDegree(self,degree):
        self.degree = degree

    def getDegree(self):
        return self.degree

    def setGrades(self, grades):
        self.grades = grades

    def getGrades(self):
        return self.grades

    def setStudentID(self, studentid):
        self.studentid = studentid

    def getStudentID(self):
        return self.studentid

    def setAge(self, age):
        self.age = age

    def getAge(self):
        return self.age


def addStudent():
        count = 0
        firstName = raw_input("Please enter the student's first name: ")
        lastName = raw_input("Please enter the student's last name: ")
        degree = raw_input("Please enter the student's degree: ")
        while count != -1:
            student.grades = raw_input("Please enter the student's module name: ")
            #student.grades["Grade"] = raw_input("Please enter the grade for %: " % grades)

        studentid = raw_input("Please enter the student's ID: ")
        age = raw_input("Please enter the student's age: ")        

        student = Student() # Create a new student object
        student.setFirstName(firstName) # Set this student's first name
        student.setLastName(lastName)
        student.setDegree(degree)
        student.setGrades(grades)
        student.setStudentID(studentid)
        student.setAge(age)

        students.append(student) # Add this student to the database
+2  A: 

A few things:

  1. Move the initialization of your class attributes into an __init__ method:

  2. Get rid of all the getters and setters as Jeffrey says.

  3. Use a dict that has module names as keys and grades as values:

Some code snippets:

def __init__(self, firstName, lastName, age, studentID, degree):
    self.firstName = firstName
    self.lastName = lastName
    ...
    self.grades = {}

and:

    while True:
        module_name = raw_input("Please enter the student's module name: ")
        if not module_name:
            break
        grade = raw_input("Please enter the grade for %s: " % module_name)
        student.grades[module_name] = grade
Ned Batchelder
+1: @Karl: That's what you need to do! Be careful: student.grades is accessed before the creation of the student object
luc
As to why you move the class attributes into the __init__ method: Declaring them within the class like that makes them (almost) static attributes -- s1 = Student(); s2 = Student(); s1.grades["foo"]=123; s2.grades will now ALSO be {"foo":123}, as will Student.grades.
AKX
@luc: thanks, that was an oversight on my part. Fixing it is left as an exercise for the OP (especially since this smells like homework...)
Ned Batchelder
@Ned: So I don't want to make the homework for Karl :) Your respone was enough information to understand class basics
luc
A: 

codelolwhatisthis? uh... i like chikn!!!! lol i iz awesome so there!!! thats my answer..... so how long does this have to go on??

meh