This article will use Python The dictionary and the list in realize the student information management system
The storage format of documents is python Self contained pickle modular , Need new course.txt and student.txt For program reading and writing .
Here is the sample code
import pickle# Read the student information from the file and return def readStudent(): with open("student.txt",'rb') as f: try: return pickle.load(f) # Read failed , Description the read file is empty , Return to the empty list except EOFError: return []# Save student information student_list To the file student.txt in def saveStudent(student_list): with open("student.txt",'wb') as f: pickle.dump(student_list, f)# Add student information def addStudent(student_list): id = input(" Please enter the student ID to be added :") name = input(" Please enter the name of the student to be added :") sex = input(" Please enter the student gender to be added :") specialty = input(" Please enter the student professional class to be added :") # Put this 4 Data loaded into the dictionary student in student = {" Student number ":id, " full name ":name, " Gender ":sex, " Professional class ":specialty} # Put the dictionary student Add to list student_list in student_list.append(student)# Delete student information def deleteStudent(student_list): id = input(" Please enter the student number to be deleted :") for i in range(0, len(student_list)): # student_list[i] It's a dictionary if student_list[i][" Student number "] == id: # Delete student_list No i A dictionary student_list.pop(i) print(" Delete successful !") return # End function print(" Delete failed , The student was not found .")# Modify student information def modifyStudent(student_list): id = input(" Please input the student number to be modified :") for i in range(0, len(student_list)): if student_list[i][" Student number "] == id: id = input(" Please enter the revised student number :") name = input(" Please enter the modified name :") sex = input(" Please enter the modified gender :") specialty = input(" Please enter the modified professional class :") # Put this 4 Data loaded into the dictionary student in student = {" Student number ":id, " full name ":name, " Gender ":sex, " Professional class ":specialty} # use student Replace student_list No i A dictionary student_list[i] = student print(" Modification successful !") return # End function print(" Modification failed , The student was not found .")# Show all student information def showStudent(student_list): print(" Student number \t\t full name \t\t Gender \t\t Professional class ") for student in student_list: print("%s\t\t%s\t\t%s\t\t%s" %(student[" Student number "], student[" full name "], student[" Gender "], student[" Professional class "]))# Read the student grade information from the file def readCourse(): with open("course.txt",'rb') as f: try: return pickle.load(f) # Read failed , Description the read file is empty , Return to the empty list except EOFError: return []# Save student grade information course_list To the file course.txt in def saveCourse(course_list): with open("course.txt",'wb') as f: pickle.dump(course_list, f)# Add student grade information def addCourse(course_list): id = input(" Please enter the student ID to be added :") english = int(input(" Please enter the student's English score :")) math = int(input(" Please enter the student's math score :")) computer = int(input(" Please enter the student's computer grade :")) chemistry = int(input(" Please enter the student's chemistry score :")) PE = int(input(" Please enter the student's physical performance :")) # Put this 6 Data loaded into the dictionary course in course = {" Student number ":id, " English ":english, " mathematics ":math, " Computer ":computer, " chemical ":chemistry, " sports ":PE} # Put the dictionary course Add to list course_list in course_list.append(course)# Delete student grade information def deleteCourse(course_list): id = input(" Please enter the student number to be deleted :") for i in range(0, len(course_list)): # course_list[i] It's a dictionary if course_list[i][" Student number "] == id: # Delete the second item in the list i A dictionary course_list.pop(i) print(" Delete successful !") return # End function print(" Delete failed , The student's grade is not found .")# Modify student grade information def modifyCourse(course_list): id = input(" Please input the student number to be modified :") for i in range(0, len(course_list)): if course_list[i][" Student number "] == id: id = input(" Please enter the revised student number :") english = int(input(" Please input the revised English score :")) math = int(input(" Please enter the revised math score :")) computer = int(input(" Please enter the modified computer score :")) chemistry = int(input(" Please enter the revised chemistry score :")) PE = int(input(" Please enter the modified sports score :")) # Put this 6 Data loaded into the dictionary course in course = {" Student number ":id, " English ":english, " mathematics ":math, " Computer ":computer, " chemical ":chemistry, " sports ":PE} # use dict Replace old data course_list[i] = course print(" Modification successful !") return # End function print(" Modification failed , The student's grade is not found .")# Display student grade information def showCourse(course_list): print(" Student number \t\t English \t\t mathematics \t\t Computer \t\t chemical \t\t sports ") for course in course_list: print("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d" %(course[" Student number "], course[" English "], course[" mathematics "], course[" Computer "], course[" chemical "], course[" sports "]))# Query the student's score information by name def searchScoreByName(student_list, course_list): name = input(" Please enter the student's name :") id = " No " for stu in student_list: if stu[" full name "] == name: id = stu[" Student number "] break if id == " No ": print(" The student information is not found .") return # End function for course in course_list: if course[" Student number "] == id: print(" The query is successful ! The student's information is as follows :") print(" Student number \t\t English \t\t mathematics \t\t Computer \t\t chemical \t\t sports ") print("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d" %(course[" Student number "], course[" English "], course[" mathematics "], course[" Computer "], course[" chemical "], course[" sports "])) return # End procedure print(" The student's grade information is not found .")# Query the student information with the highest score in a course def searchStudentByHighScore(student_list, course_list): courseName = input(" Please enter the course name :") # According to this course bubble sort for i in range(0, len(course_list)-1): for j in range(0, len(course_list)-1-i): if course_list[j][courseName] < course_list[j+1][courseName]: course_list[j], course_list[j+1] = course_list[j+1], course_list[j] id = course_list[0][" Student number "] for student in student_list: if student[" Student number "] == id: print(" Find success !") print(" Student number \t\t full name \t\t Gender \t\t Professional class ") print("%s\t\t%s\t\t%s\t\t%s" %(student[" Student number "], student[" full name "], student[" Gender "], student[" Professional class "])) return print(" The student information was not found .")# Output the average score of a course def outputAverageScore(course_list): courseName = input(" Please enter the course name :") sum = 0 for course in course_list: sum += course[courseName] print("%s The average score is %.2f" %(courseName, sum/len(course_list)))# The main menu def menu(): print("1. Add student information 2. Add score information ") print("3. Modify student information 4. Modify score information ") print("5. Delete student information 6. Delete score information ") print("7. Show student information 8. Show grade information ") print("9. Query the student's score information by name ") print("10. Query the student information with the highest score in a course ") print("11. Output the average score of a course ") print("0. Save and exit ")# The main function def main(): student_list = readStudent() course_list = readCourse() flag = True while flag: menu() choose = input(" Please enter the option :") while True: # Add student information if choose == '1': addStudent(student_list) break # Add score information elif choose == '2': addCourse(course_list) break # Modify student information elif choose == '3': modifyStudent(student_list) break # Modify score information elif choose == '4': modifyCourse(course_list) break # Delete student information elif choose == '5': deleteStudent(student_list) break # Delete score information elif choose == '6': deleteCourse(course_list) break # Show student information elif choose == '7': showStudent(student_list) break # Show grade information elif choose == '8': showCourse(course_list) break # Query the student's score information by name elif choose == '9': searchScoreByName(student_list, course_list) break # Query the student information with the highest score in a course elif choose == '10': searchStudentByHighScore(student_list, course_list) break # Output the average score of a course elif choose == '11': outputAverageScore(course_list) break # Save the data and exit elif choose == '0': saveStudent(student_list) saveCourse(course_list) flag = False break else: choose = input(" Input error , Please re-enter :")if __name__=='__main__': main()
This is about Python The use of dictionaries and lists to achieve student information management system is introduced here , More about Python Please search the previous articles of the software development network or continue to browse the relevant articles below for the content of the student information management system. I hope you will support the software development network in the future !