简体   繁体   English

Python raw_input到在类中声明的字典中

[英]Python raw_input into dictionary declared in a class

I am incredibly new to Python and I really need to be able to work this out. 我是Python的新手,我真的需要能够解决这个问题。 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. 我想通过raw_input询问用户什么模块和年级,然后将其放入已经在Student类中定义为年级的字典中。 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

A few things: 一些东西:

  1. Move the initialization of your class attributes into an __init__ method: 将类属性的初始化移到__init__方法中:

  2. Get rid of all the getters and setters as Jeffrey says. 摆脱Jeffrey所说的所有获取方法和方法。

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM