python Student Information Management System (Full version)
- 2020-12-20 03:40:52
- OfStack
This paper is based on the previous one (python project: Student Information Management System (first edition)), and has been improved and added new functions.
It mainly includes:
Perfect part: Input error; There are no data queries and other exception errors
New features: Operations on files: reads and writes to files, with emphasis on detailed parsing of text strings (see the code for full parsing and reorganization, as well as adding comments)
Student Information Management System (Full version)
Student information management project, with operation interface, and complete each operation:
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
| 1) add student information |
| 2) display all student information |
| 3) delete student information |
| 4) modify student information |
| 5) display | of student information according to high - low student scores
| 6) display | of student information according to low to high scores of students
| 7) display | of student information according to student age
| 8) display | of student information according to low - high student age
| 9) save student information to file (students.txt) |
| 10) reads data from a file (students.txt) |
| exit: any other button < enter > |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
The detailed code is as follows:
# student_info.py
# Student information management project, with operation interface, and complete each operation:
# + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
# | 1) add student information |
# | 2) display all student information |
# | 3) delete student information |
# | 4) modify student information |
# | 5) display | of student information according to high - low student scores
# | 6) display | of student information according to low to high scores of students
# | 7) display | of student information according to student age
# | 8) display | of student information according to low - high student age
# | 9) Save student information to the file ( students.txt) |
# | 10) Read data from a file ( students.txt) |
# | Exit: any other key < enter > |
# + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + |
def meun():
menu_info = ''' + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
| 1) Add student information |
| 2) Display the information of all students |
| 3) Delete student information |
| 4) Modify student information |
| 5) Display student information according to high - low scores |
| 6) Display student information according to low - high student performance |
| 7) Display student information according to student age |
| 8) Display student information by age |
| 9) Save student information to the file ( students.txt) |
| 10) Read data from a file ( students.txt) |
| Exit: any other key < enter > |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
'''
print(menu_info)
# The following 2 A function is used to sorted Sorting, key Expression function of
def get_age(*l):
for x in l:
return x.get("age")
def get_score(*l):
for x in l:
return x.get("score")
# 1) Add student information
def add_student_info():
L = []
while True:
n = input(" Please enter your name: ")
if not n: # Jump out of the loop with the name null
break
try:
a = int(input(" Please enter your age: "))
s = int(input(" Please enter your score: "))
except:
print(" Invalid input, not shaping value... Reentry information ")
continue
info = {"name":n,"age":a,"score":s}
L.append(info)
print(" Student information input finished!! ")
return L
# 2) Display the information of all students
def show_student_info(student_info):
if not student_info:
print(" According to countless sources... ")
return
print(" The name ".center(8)," age ".center(4)," results ".center(4))
for info in student_info:
print(info.get("name").center(10),str(info.get("age")).center(4),str(info.get("score")).center(4))
# 3) Delete student information
def del_student_info(student_info,del_name = ''):
if not del_name:
del_name = input(" Please enter the deleted student name: ")
for info in student_info:
if del_name == info.get("name"):
return info
raise IndexError(" Student information does not match , Could not find %s" %del_name)
# 4) Modify student information
def mod_student_info(student_info):
mod_name = input(" Please enter the modified student name: ")
for info in student_info:
if mod_name == info.get("name"):
a = int(input(" Please enter your age: "))
s = int(input(" Please enter your score: "))
info = {"name":mod_name,"age":a,"score":s}
return info
raise IndexError(" Student information does not match , Could not find %s" %mod_name)
# 5) Display student information according to high - low scores
def score_reduce(student_info):
print(" Students are shown by high - low scores ")
mit = sorted(student_info ,key = get_score,reverse = True)
show_student_info(mit)
# 6) Display student information according to low - high student performance
def score_rise(student_info):
print(" Show students' low - high scores ")
mit = sorted(student_info ,key = get_score)
show_student_info(mit)
# 7) Display student information according to student age
def age_reduce(student_info):
print(" High and low by student age: ")
mit = sorted(student_info ,key = get_age,reverse = True)
show_student_info(mit)
# 8) Display student information by age
def age_rise(student_info):
print(" Low to high by student age: ")
mit = sorted(student_info ,key = get_age)
show_student_info(mit)
# 9) Save student information to the file ( students.txt)
def save_info(student_info):
try:
students_txt = open("students.txt","w") # Open in write mode and empty the contents of the file
except Exception as e:
students_txt = open("students.txt", "x") # The file does not exist. Create the file and open it
for info in student_info:
students_txt.write(str(info)+"\n") # Store by line, adding a newline character
students_txt.close()
# 10) Read data from a file ( students.txt)
def read_info():
old_info = []
try:
students_txt = open("students.txt")
except:
print(" No data information has been saved ") # Failed to open. File does not exist. No data saved
return
while True:
info = students_txt.readline()
if not info:
break
# print(info)
info = info.rstrip() # Get rid of newline characters
# print(info)
info = info[1:-1] # Remove the {}
# print(info)
student_dict = {} # Individual student dictionary information
for x in info.split(","): # Split at intervals of theta and theta
# print(x)
key_value = [] # Open up space, key_value[0] save key,key_value[0] save value
for k in x.split(":"): # Divide by:
k = k.strip() # Remove the first and last null characters
# print(k)
if k[0] == k[-1] and len(k) > 2: # Determines whether it is a string or an integer
key_value.append(k[1:-1]) # Get rid of the first and the last.
else:
key_value.append(int(k))
# print(key_value)
student_dict[key_value[0]] = key_value[1] # Student information addition
# print(student_dict)
old_info.append(student_dict) # Summary of all student information
students_txt.close()
return old_info
def main():
student_info = []
while True:
# print(student_info)
meun()
number = input(" Please enter options: ")
if number == '1':
student_info = add_student_info()
elif number == '2':
show_student_info(student_info)
elif number == '3':
try:
student_info.remove(del_student_info(student_info))
except Exception as e:
# The student names do not match
print(e)
elif number == '4':
try:
student = mod_student_info(student_info)
except Exception as e:
# The student names do not match
print(e)
else:
# First, delete the student from the list according to the name entered, and then re-add the student's latest information
student_info.remove(del_student_info(student_info,del_name = student.get("name")))
student_info.append(student)
elif number == '5':
score_reduce(student_info)
elif number == '6':
score_rise(student_info)
elif number == '7':
age_reduce(student_info)
elif number == '8':
age_rise(student_info)
elif number == '9':
save_info(student_info)
elif number == '10':
student_info = read_info()
else:
break
input(" Enter displays the menu ")
main()
For more information about the management system, please click "Management system Topics" to learn