2015-12-04 43 views
1

取消時輸入錯誤我保持這個簡短。我有一個Python類文件和一個常規文件。 課程是學生,課程的重點是可以改變某些屬性,添加學生並在學生後顯示GPA。 這裏是程序文件:從students.dat

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 

錯誤:

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 

回答

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) 

按照慣例類大寫和實例是小寫。這有助於瞭解正在發生的事情。

+0

這使得更多的意義。謝謝!而即時通訊猜測,如果我要添加更多的學生for循環這個新的學生實例將創建新的對象,並將它們添加到我的students.dat文件? –

+0

是的,「Student」類可以創建儘可能多的實例。在循環中使用'pickle.dump()'會依次挑選所有對象。用pickle.load()'循環取消它們,讓它們全部回來。 –

+0

一個問題的參數IM發送到我的節目GPA的方法是返回類型錯誤 高清showGPA(studentObj): 名稱=輸入(「輸入名稱:」) 如果名稱中studentObj: 進入=名稱.get_GPA() print(entry) –