为什么没有定义一些变量?

时间:2015-04-25 06:56:20

标签: python dictionary

这就是我要做的事情:

查找并打印学生GPA

将新学生添加到班级

更改学生的GPA

更改学生的预期成绩

以表格格式打印所有学生的数据

退出程序

    import student
    import pickle

    lookup = 1
    change = 2
    add = 3
    delete = 4
    QUIT = 0
    FILENAME = 'student.dat'

    def main():

        students_info = load_students()
        choice = 0
        load_students()
        #add(students_info)
        change_grade(students_info)
        change_GPA(students_info)
        #get_menu_choice()
        look_up(students_info)
        while choice != QUIT:
            choice = get_menu_choice()
            if choice == lookup:
                look_up(students_info)
            elif choice == add:
                add(students_info)
            elif choice == change:
                change(students_info)
            elif choice == delete:
                delete(students_info)
        save_students(students_info)


    def load_students():
        try:
            input_file = open(FILENAME, 'rb')
            students_dict = pickle.load(input_file)
            input_file.close()
        except IOError:
            students_dict = {}
        print(students_dict)
        return students_dict

    def get_menu_choice():
        print()
        print('Menu')
        print("-------------------")
        print('1. Look up ID')
        print('2.....')
        choice = int(input("Enter your choice:"))

        return choice

    def look_up(students_info):
        ID = input('Enter ID:')
        print(student_info.get(ID, "Not found!"))
    ##    try:
    ##        print(students_info[ID])
    ##    except KeyError:
    ##        print("Not found!")

    def change_GPA(students_info):
        ID = input("ID:")
        if ID in students_info:
            GPA= float(input("New GPA:"))
            students=student.Student(ID,GPA,grade,work)
            students_info[ID] = students
            print ("This",students_info[ID])
        else:
            print("Not found!")
    def change_grade(students_info):
        ID = input("ID:")
        if ID in students_info:
            New_grade = input("Enter new grade:")
            students=student.Student(ID,GPA,grade,work)
            students_info[ID] = students
            #new_grade = students_info[name]
        else:
            print("Not found!")

    def add(students_info):
        name = input("Enter the student name:")
        ID= input("Enter student's ID:")
        GPA= float(input("Enter GPA:"))
        grade= input("Enter student's expected grade:")
        work = input("Does the student work part time or full time?")
        students=student.Student(name,ID,GPA,grade,work)
        print(students_info['ID'])
    def save_students(students_info):
        output_file = open(FILENAME, 'wb')
        pickle.dump(students_info, output_file)
        output_file.close()
    main()

每当我尝试更改GPA或等级时,都没有定义。我怎样才能从字典studens_info中更改一个值?

1 个答案:

答案 0 :(得分:0)

正如gus42所评论的那样,你得到的错误来自以下几行:

students=student.Student(ID,GPA,grade,work)

您在任何一个地方都没有定义gradeworkchange_grade中也没有定义GPA),因此您不应该感到惊讶;得到错误。

我认为有两种方法可以解决这个问题:

最简单的方法是将逻辑从创建新Student对象更改为修改已存在的对象。我不确切知道Student课程中属性名称的确切含义,但这是对解决方案的合理猜测:

def change_GPA(students_info):
    ID = input("ID:")
    if ID in students_info:
        GPA= float(input("New GPA:"))
        students_info[ID].GPA = GPA  # assign new GPA to old student object

    else:
        print("Not found!")

def change_grade(students_info):
    ID = input("ID:")
    if ID in students_info:
        grade = input("Enter new grade:")
        students_info[ID].grade = grade  # assign new grade to existing student object
    else:
        print("Not found!")

另一个选项是用一个具有不同值的新对象替换现有的Student对象。这与您当前的代码很接近,但只有Student对象是不可变的(可能是因为您使用namedtuple创建了类型?)才真正有意义。为了使其工作(当前代码没有),您必须在创建新对象之前从旧Student对象加载旧值:

def change_GPA(students_info):
    ID = input("ID:")
    if ID in students_info:
        new_GPA = float(input("New GPA:"))
        old_grade = students_info[ID].grade  # load old values
        old_work = students_info[ID].work
        new_student = students.Student(ID, new_GPA, old_grade, old_work)
        students_info[ID] = new_student  # replace the old student with a new object
    else:
        print("Not found!")

def change_grade(students_info):
    ID = input("ID:")
    if ID in students_info:
        new_grade = input("Enter new grade:")
        old_GPA = students_info[ID].GPA
        old_work = students_info[ID].work
        new_student = students.Student(ID, old_GPA, new_grade, old_work)
        students_info[ID] = new_student
    else:
        print("Not found!")