开发者

Python raw_input into dictionary declared in a class

开发者 https://www.devze.com 2022-12-13 12:55 出处:网络
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 d

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 student开发者_Go百科s\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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消