Student management system is implemented based on python
- 2020-12-20 03:40:35
- OfStack
This paper shares the specific code of python student management system for your reference. The specific content is as follows
Version 1.0 student Management System
'''
1. Add the student
2. Modify the students
3. Query students
4. Delete the students
0. Exit the program
'''
student_list=[]
while True:
print('1. Add the student ')
print('2. Modify the students ')
print('3. Query students ')
print('4. Delete the students ')
print('0. Exit the program ')
sel_num=input(' Please enter what you want to do: ')
sel_num=int(sel_num)
# If the selected number is not 0~5 Continue to choose
while sel_num not in range(0,5):
sel_num=input(' Your selection is invalid, please re-select: ')
sel_num=int(sel_num)
if sel_num==1:
name=input(' Please enter your name: ')
age=input(' Please enter your age: ')
sex=input(' Please enter gender: ')
person_list=[name,age,sex]
student_list.append(person_list)
print(' Added successfully! ')
elif sel_num==2:
for x in range(0,len(student_list)):
person=student_list[x]
print(' Serial number: %s Name: %s age :%s gender :%s '%(x,person[0],person[1],person[2]))
index=input(' Please enter the serial number you want to modify: ')
index=int(index)
while index not in range(0, len(student_list)):
index = input(' The serial number you selected is invalid. Please re-select: ')
index = int(index)
person=student_list[index]
name=person[0]
age=person[1]
sex=person[2]
student_list[index][0]=input(' Please enter the modified name: (%s):'%name)
student_list[index][1]=input(' Please enter the modified age: (%s):'%age)
student_list[index][2]=input(' Please enter the modified gender: (%s)'%sex)
print(' Modified successfully! ')
elif sel_num==3:
for x in range(0,len(student_list)):
person=student_list[x]
name=person[0]
age=person[1]
sex=person[2]
print(' Serial number: %s Name: %s age :%s gender :%s '%(x,name,age,sex))
elif sel_num==4:
for x in range(0,len(student_list)):
person=student_list[x]
print(' Serial number: %s Name: %s age :%s gender :%s '%(x,person[0],person[1],person[2]))
print('1. Delete all trainees ')
print('2. Delete the selected student ')
num=input(' Please enter your choice: ')
if num=='1':
student_list.clear()
else:
index = input(' Please enter the serial number of the student to delete: ')
index = int(index)
while index not in range(0, len(student_list)):
index = input(' The serial number you selected is invalid. Please re-select: ')
index = int(index)
del student_list[index]
else:
break
Version 2.0 student management system - Function version - uses lists to store student information
# Add student function
def add_student():
# Enter the student's name, age and telephone number
name=input(' Please enter student name: ')
age=input(' Please enter student age: ')
phone=input(' Please enter the student's telephone number: ')
# the name , age , phone Put it in a little list
student=[name,age,phone]
# Add the small list to the large list of all trainees
# append(object) insert(index,object) extend(iterable)
student_list.append(student)
print(' Add trainee success! ')
# Query the student function
def query_student():
#1. Query all trainees
#2. Enter student name Query trainees to get the serial number of the query trainees
print('1. Query all trainees ')
print('2. Query some trainees ')
num=int(input(' Please enter the operation sequence number: '))
while num not in range(1,3):
num=int(input(' Invalid selection, please re-enter: '))
if num==1:
print('************** Student Information list ***************')
# Iterate through the large list
for x in range(0,len(student_list)):
# According to the x From the large list to the small list
student=student_list[x]
# Remove your name, age, and phone number from the list
name=student[0]
age=student[1]
phone=student[2]
print(' The serial number :%s The name :%s age :%s The phone :%s'%(x,name,age,phone))
else:
name = input(' Please enter the name of the student you want to check: ')
while 1:
a=False
for student in student_list:
if student[0] == name:
index = student_list.index(student, 0, 8)
print(' Serial number: %s Name: %s age :%s The phone :%s'%(index,student_list[index][0],student_list[index][1],student_list[
index][2]))
a=True
if a==False:
name=input(' The student was not found, please re-enter: ')
else:
break
# Modify the student's function
def update_student():
# Determines if there is any student information, and if not, terminates the function execution
if len(student_list)==0:
print(' No student information, can not modify the operation! ')
# Force an end to function execution return None of the following code will execute again
return
#1. Query student information
query_student()
#2. Select the trainee number you want to modify
num=input(' Please select the student number you want to modify: ')
#3. Convert to integer
num=int(num)
#4. Determines whether the selected student serial number is in the range
while num not in range(0,len(student_list)):
# Out of range, reselect
num=input(' There is no such serial number, please re-select: ')
num=int(num)
#5. Extract the corresponding small list according to the selected sequence number
student=student_list[num]
new_name=input(' Please enter the modified name (%s) : '%student[0])
new_age=input(' Please enter the modified age (%s)'%student[1])
new_phone=input(' Please enter the modified phone number (%s)'%student[2])
#6. Modify the data in the small list
student[0]=new_name
student[1]=new_age
student[2]=new_phone
print(' Modify data complete! ')
# Delete the students
#1. Delete according to student serial number 2. Delete all trainees 3. Delete according to the student's name ( There are of the same name )
def delete_student():
if len(student_list)==0:
print(' No student information, can not perform delete operation! ')
return
print('1. Delete according to student serial number ')
print('2. Delete all trainees ')
print('3. Delete students according to their names ')
# Gets the input and converts it to an integer type
num=int(input(' Please enter your choice: '))
# Determine if the selected option is within range
while num not in range(1,4):
num=int(input(' There is no serial number, please select it again '))
# Determine the selected option
if num == 1:
# 1. Query student information
query_student()
#2. Select the number to delete
num=int(input(' Please enter the student number you want to delete: '))
# Determine if the selection number is in range
while num not in range(0,len(student_list)):
num=int(input(' The serial number is invalid, please reselect it! '))
is_del=input(' You are sure you want to delete (%s) Student information? (y/n):'%student_list[num][0])
if is_del=='y':
# Delete all data in the list
del student_list[num]
#student_list.pop(index)
print('%s Student information deleted successfully! '%student_list[num][0])
if num==2:
# Confirm the deletion
is_del=input(' Are you sure you want to delete all student information? y( determine )/n( cancel ):')
if is_del=='y':
# Delete all data in the list
student_list.clear()
print(' All students deleted successfully! ')
else:
print(' Delete data operation cancelled! ')
else:
name = input(' Please enter the name of the student you want to delete: ')
while 1:
# Definition list storage is not equal to name The list of small
list = []
# Iterate through the large list
for student in student_list:
# Judging input name Whether and small list name The equality of
if student[0] != name:
# To find out and name The index in which the unequal small lists reside
index = student_list.index(student,0,len(student_list))
# Adds a small list of matches to list In the list
list.append(student_list[index])
# Determine if two lists are the same length Equality means there is no name in the large list name The list of small
if len(student_list) == len(list):
name = input(' The serial number does not exist, please re-enter: ')
# There is a small list of matches
else:
# Clear the list
student_list.clear()
# Loop will list The contents of the list are written to an empty large list
for dict in list:
student_list.append(dict)
break
#1. The statement 1 A large list of all student information
student_list=[]
#2.while cycle
while True:
print('*********** Student management system V2.0**************')
print('1. Add the student ')
print('2. Query students ')
print('3. Modify the students ')
print('4. Delete the students ')
print('0. Exit the program ')
print('***************************************')
# Select operation
num=input(' Please select your operation: ')
# Convert to integer
num=int(num)
# Determine if the selected option is within range
while num not in range(0,5):
# To choose
num=input(' This option is not available, please reselect: ')
# Converts a string to an integer type
num=int(num)
# Perform the corresponding action based on the selected option
if num==1:
# Calls the function that adds the student
add_student()
elif num==2:
# Call the function that queries the student
query_student()
elif num==3:
# Calls the function that modifies the student's
update_student()
elif num==4:
delete_student()
else:
print(' Program is over !')
break
2.1 Student Management system -- Function version -- use dictionary to store student information
# Add student function
def add_student():
# Enter the student's name, age and telephone number
name=input(' Please enter student name: ')
age=input(' Please enter student age: ')
phone=input(' Please enter the student's telephone number: ')
# the name , age , phone Put it in a small dictionary
student={'name':name,'age':age,'phone':phone}
# Add the small dictionary to the large list of all students
# append(object) insert(index,object) extend(iterable)
student_list.append(student)
print(' Add trainee success! ')
# Query the student function
def query_student():
#1. Query all trainees
#2. Enter student name Query trainees to get the serial number of the query trainees
print('1. Query all trainees ')
print('2. Query some trainees ')
num=int(input(' Please enter the operation sequence number: '))
while num not in range(1,3):
num=int(input(' Invalid selection, please re-enter: '))
if num==1:
print('************** Student Information list ***************')
# Iterate through the large list
for x in range(0,len(student_list)):
# According to the x Take the small dictionary from the large list
student=student_list[x]
# Remove your name, age, and phone number from the small dictionary
name=student['name']
age=student['age']
phone=student['phone']
print(' The serial number :%s The name :%s age :%s The phone :%s'%(x,name,age,phone))
else:
name = input(' Please enter the name of the student you want to check: ')
while 1:
a=False
for student in student_list:
if student['name'] == name:
index = student_list.index(student, 0, 8)
print(' Serial number: %s Name: %s age :%s The phone :%s'%(index,student_list[index]['name'],student_list[index]['age'],
student_list[index]['phone']))
a=True
if a==False:
name=input(' The student was not found, please re-enter: ')
else:
break
# Modify the student's function
def update_student():
# Determines if there is any student information, and if not, terminates the function execution
if len(student_list)==0:
print(' No student information, can not modify the operation! ')
# Force an end to function execution return None of the following code will execute again
return
#1. Query student information
query_student()
#2. Select the trainee number you want to modify
num=input(' Please select the student number you want to modify: ')
#3. Convert to integer
num=int(num)
#4. Determines whether the selected student serial number is in the range
while num not in range(0,len(student_list)):
# Out of range, reselect
num=input(' There is no such serial number, please re-select: ')
num=int(num)
#5. Take out the corresponding small dictionary according to the selected sequence number
student=student_list[num]
new_name=input(' Please enter the modified name (%s) : '%student['name'])
new_age=input(' Please enter the modified age (%s)'%student['age'])
new_phone=input(' Please enter the modified phone number (%s)'%student['phone'])
#6. Modify the data in the small list
student['name']=new_name
student['age']=new_age
student['phone']=new_phone
print(' Modify data complete! ')
# Delete the students
#1. Delete according to student serial number 2. Delete all trainees 3. Delete according to the student's name ( There are of the same name )
def delete_student():
if len(student_list)==0:
print(' No student information, can not perform delete operation! ')
return
print('1. Delete according to student serial number ')
print('2. Delete all trainees ')
print('3. Delete students according to their names ')
# Gets the input and converts it to an integer type
num=int(input(' Please enter your choice: '))
# Determine if the selected option is within range
while num not in range(1,4):
num=int(input(' There is no serial number, please select it again '))
# Determine the selected option
if num == 1:
# 1. Query student information
query_student()
#2. Select the number to delete
num=int(input(' Please enter the student number you want to delete: '))
# Determine if the selection number is in range
while num not in range(0,len(student_list)):
num=int(input(' The serial number is invalid, please reselect it! '))
is_del=input(' You are sure you want to delete (%s) Student information? (y/n):'%student_list[num]['name'])
if is_del=='y':
# Delete all data in the list
del student_list[num]
#student_list.pop(index)
print('%s Student information deleted successfully! '%student_list[num]['name'])
else:
print(' Delete data operation cancelled! ')
elif num==2:
# Confirm the deletion
is_del=input(' Are you sure you want to delete all student information? y( determine )/n( cancel ):')
if is_del=='y':
# Delete all data in the list
student_list.clear()
print(' All students deleted successfully! ')
else:
print(' Delete data operation cancelled! ')
else:
name = input(' Please enter the name of the student you want to delete: ')
while 1:
# Definition list storage is not equal to name The list of small
list=[]
# Iterate through the large list
for student in student_list:
# Judging input name Whether and small dictionary name The equality of
if student['name']!=name:
# To find out and name The index in which different small dictionaries are located
index=student_list.index(student)
# Adds a matching small dictionary to list In the list
list.append(student_list[index])
# Determine if two lists are the same length Equality means there is no name in the large list name The list of small
if len(student_list)==len(list):
name=input(' The serial number does not exist, please re-enter: ')
# There are little dictionaries that match
else:
# Clear the list
student_list.clear()
# Loop will list The contents of the list are written to an empty large list
for dict in list:
student_list.append(dict)
break
#1. The statement 1 A large list of all student information
student_list=[]
#2.while cycle
while True:
print('*********** Student management system V2.0**************')
print('1. Add the student ')
print('2. Query students ')
print('3. Modify the students ')
print('4. Delete the students ')
print('0. Exit the program ')
print('***************************************')
# Select operation
num=input(' Please select your operation: ')
# Convert to integer
num=int(num)
# Determine if the selected option is within range
while num not in range(0,5):
# To choose
num=input(' This option is not available, please reselect: ')
# Converts a string to an integer type
num=int(num)
# Perform the corresponding action based on the selected option
if num==1:
# Calls the function that adds the student
add_student()
elif num==2:
# Call the function that queries the student
query_student()
elif num==3:
# Calls the function that modifies the student's
update_student()
elif num==4:
delete_student()
else:
print(' Program is over !')
break
Version 3.0 Student Management system - saves student information to a file
def is_in_range():
index = input(' Please choose to ( Modify the ) Student Serial number deleted: ')
index = int(index)
while index not in range(0, len(student_list)):
index = input(' The serial number you entered is not in the range, please re-enter: ')
index = int(index)
return index
def add_stu():
name = input(' Please enter your name: ')
age = input(' Please enter your age: ')
sex = input(' Please enter gender: ')
person_list = [name, age, sex]
student_list.append(person_list)
print(' Added successfully! ')
def alter_stu():
index=is_in_range()
person = student_list[index]
name = person[0]
age = person[1]
sex = person[2]
student_list[index][0] = input(' Please enter the modified name: (%s):' % name)
student_list[index][1] = input(' Please enter the modified age: (%s):' % age)
student_list[index][2] = input(' Please enter the modified gender: (%s)' % sex)
print(' Modified successfully! ')
def see_stu():
for x in range(0, len(student_list)):
person = student_list[x]
name = person[0]
age = person[1]
sex = person[2]
print(' Serial number: %s Name: %s age :%s gender :%s ' % (x, name, age, sex))
def del_stu():
print('1. Delete all trainees ')
print('2. Delete the selected student ')
num = input(' Please enter your choice: ')
if num == '1':
student_list.clear()
else:
index = is_in_range()
del student_list[index]
# Declare the function that holds the data
def save_data():
#1. Open the file
file_handle=open('student_v2.txt',mode='w')
#2.for Loop through the large list
for student in student_list:
# Splice the data in the list with whitespace 1 A string
s=' '.join(student)
# write
file_handle.write(s)
file_handle.write('\n')
#3. Close the file
file_handle.close()
# The introduction of os The module
import os
# A function that reads a file
def read_data():
# Determine if the file exists
rs=os.path.exists('student_v2.txt')
if rs==True:
#1. Open the file
file_handle=open('student_v2.txt',mode='r')
#2. Take out the information
contents=file_handle.readlines()
for content in contents:
# Get rid of /n
content=content.strip('\n')
# Use whitespace to split the string and get the list
list_1=content.split(' ')
# Adds a small list to a large list
student_list.append(list_1)
# 3. Close the file
file_handle.close()
# The statement 1 A large list of the names of colleges
student_list = []
read_data()
while True:
print('1. Add the student ')
print('2. Modify the students ')
print('3. Query students ')
print('4. Delete the students ')
print('0. Exit the program ')
sel_num = input(' Please enter what you want to do: ')
sel_num = int(sel_num)
# If the selected number is not 0~5 Continue to choose
while sel_num not in range(0, 5):
sel_num = input(' Your selection is invalid, please re-select: ')
sel_num = int(sel_num)
# Add the student
if sel_num == 1:
add_stu()
save_data()
elif sel_num == 2:
# 1. Display all student information
see_stu()
# 2. Select the trainee number you want to modify
alter_stu()
save_data()
elif sel_num == 3:
see_stu()
elif sel_num == 4:
see_stu()
# 2. Select the student number you want to delete
del_stu()
save_data()
else:
break