从students.dat取消选择时输入错误

时间:2015-12-04 08:15:38

标签: python class typeerror instances

我要保持简短。我有一个Python类文件和一个常规文件。 课程是学生,课程的重点是能够改变一些属性,添加学生并在学生中显示GPA。 这是程序文件:

import studentClass
import pickle

student = studentClass.Student
FILENAME = "students.dat"

LOOK_UP = 1
ADD = 2
CHANGE_GPA = 3
CHANGE_GRADE = 4
DISPLAY = 5
QUIT = 6

#makes Students
def make_students():



    print("Enter data for students")
    output_file = open(FILENAME, "wb")
    for count in range(1,2):

        print("Student " +str(count) + ':')
        name = input("Enter student Name: ")
        stuID = int(input("Enter student ID:"))
        GPA = float(input("Enter GPA: "))
        expectGrade = (input("Enter the expected grade: "))
        fTime_pTime = input("Is the student full-time or part-time: ")

        student(name,stuID, GPA, expectGrade, fTime_pTime)

        pickle.dump(student, output_file)

    output_file.close()

    main()

#Shows GPA of student
def showGPA(students):
    name = input("enter a name: ")

    if name in students:
        entry = student.get_GPA()
        print(entry)

#adds and student

def add(students):

    name = input("Enter student Name: ")
    stuID = int(input("Enter student ID:"))
    GPA = float(input("Enter GPA: "))
    expectGrade = (input("Enter the expected grade: "))
    fTime_pTime = input("Is the student full-time or part-time: ")

    entry = student(name,stuID, GPA, expectGrade, fTime_pTime)

    if name not in students:
        students[name] = entry
        print('Entry has been added')
    else:
        print('That name is taken')



#Changes GPA
def changeGPA(students):
    name = input("Enter a name: ")

    if name in students:
        GPA = input("enter a new GPA")

        entry = student.set_GPA(GPA)

        students[name] = entry
        print("Info updated")

    else:
        print("That name was not found")


#Changes expected grade
def changeEX(students):
    name = input("Enter a name: ")

    if name in students:
        expectGrade = input("enter a new GPA")

        entry = student.set_expectGrade(expectGrade)

        students[name] = entry
        print("Info updated")

    else:
        print("That name was not found")


#Saves changes
def save_students(student):

    output_file = open(FILENAME, 'wb')

    pickle.dump(student, output_file)

    output_file.close()


#Displays list of students
def display_list(student_list):
    for item in student_list:
        print(item.get_name())
        print(item.get_stuID())
        print(item.get_GPA)
        print(item.get_expectGrade)
        print(item.get_fulltime)

#Loads the students.dat
def load_students():
    try:
        input_file = open(FILENAME, 'rb')

        student_dct = pickle.load(input_file)

        input_file.close()

    except IOError:

        student_dct = {}

    return student_dct

def make_menu():
    print("1. Look up Students GPA.")
    print("2. Add a new student.")
    print("3. Change GPA for a student.")
    print("4. Change expected grade of a student")
    print("5. Display all student.")
    print("6. Quit Program.")
    choice = int(input("Enter your choice: "))

    while choice < LOOK_UP or choice > QUIT:
        choice = int(input("Enter a vaild choice: "))

    return choice

def main():

    #pickle_student.main()
    #make_students()

    studentObjs = load_students()
    choice = make_menu()

    while choice != QUIT:

        if choice == LOOK_UP:
            showGPA(studentObjs)
        elif choice == ADD:
            add(studentObjs)
        elif choice == CHANGE_GPA:
            changeGPA(studentObjs)
        elif choice == CHANGE_GRADE:
            changeEX(studentObjs)
        elif choice == DISPLAY:
            display_list(studentObjs)

    save_students(studentObjs)


#runs program
make_students()

AND ...类文件

__author__ = 'MichaelCorbett'

class Student:

    def __init__(self,name,stuID, GPA, expectGrade, fulltime):

        self.__name = name
        self.__studID = stuID
        self.__GPA = GPA
        self.__expectGrade = expectGrade
        self.__fulltime = fulltime


    # Set methods
    def set_name(self,name):
        self.__name = name

    def set_stuID(self, stuID):
        self.__studID

    def set_GPA(self, GPA):
        self.__GPA = GPA

    def set_expectGrade(self, expectGrade):
        self.__expectGrade

    def set_fulltime(self, fulltime):
        self.__fulltime = fulltime


    # Get methods
    def get_name(self):
        return self.__name

    def get_stuID(self):
        return self.__studID

    def get_GPA(self):
        return self.__GPA

    def get_expectGrade(self):
        return self.__expectGrade

    def get_fulltime(self):
        return self.__fulltime

    def __str__(self):
        return "Student: " + self.__name + \
            "\nStudent ID: " + self.__studID+ \
            "\nGPA: "+ self.__GPA+ \
            "\nExpected Grade: " + self.__expectGrade+\
            "\nFullTime: " + self.__fulltime

ERROR:

Traceback (most recent call last):
  File "/Users/MichaelCorbett/PycharmProjects/lab9/test.py", line 171, in <module>
    make_students()
  File "/Users/MichaelCorbett/PycharmProjects/lab9/test.py", line 37, in make_students
    main()
  File "/Users/MichaelCorbett/PycharmProjects/lab9/test.py", line 165, in main
    display_list(studentObjs)
  File "/Users/MichaelCorbett/PycharmProjects/lab9/test.py", line 110, in display_list
    for item in student_list:
TypeError: 'type' object is not iterable

1 个答案:

答案 0 :(得分:0)

在您的函数make_students()中,不要挑选课程student

    student(name,stuID, GPA, expectGrade, fTime_pTime)
    pickle.dump(student, output_file)

挑选创建的实例:

    one_student = student(name,stuID, GPA, expectGrade, fTime_pTime)
    pickle.dump(one_student, output_file)

最好保持班级大写:

from studentClass import Student

现在你可以写:

student = Student(name,stuID, GPA, expectGrade, fTime_pTime)
pickle.dump(student, output_file)

按照约定类,大写和实例是小写的。这有助于了解正在发生的事情。