Teach you to use Python to realize the simple version of student information management system of with source code

  • 2021-11-13 02:22:28
  • OfStack

1. Project analysis

1. First, define a list for storing basic student information

Format: list = [{student number: name, age: num, grade: num}, {…}]


student1 = [\
            {1:"jam",'age':17,'socer':99},
            {2:'kol','age':18,'socer':89},
            {3:'avlir','age':19,'socer':100}]

2. Build the framework first (main function main ())


if __name__ == '__main__':
    while True:
        menu()
        key = input(" Enter the number of options   Query / Input   Information: ")
        if key == '1':
            lookup() # Print the specified student information 
        elif key == '2':
            append() # Add new student information 
        elif key == '3':
            delete() # Delete specified student information 
        elif key == '4':
            allinformation() # Print all information 
        elif key == '5':
            revise() # Modify the specified student information 
        elif key == '6':
            save() # Save information 
        elif key == '0':
            print(" Are you sure to quit? ")
            exit = input(" Input yes Exit: ")
            if exit == 'yes':
                break
            else:
                print(" Enter error, return  ")

3. After building the framework, it is found that a menu interface is needed (menu ())


# Page 
def menu():
    print('-'*30)
    print(" Welcome to Student Management System ")
    print("1. Enter the student number to inquire about the results ")
    print("2. Add Student Information ")
    print("3. Delete student information ")
    print("4. Print all student information ")
    print("5. Modify student information ")
    print("6. Save student information to specified file ")
    print("0. Exit the system ")
    print("-"*30)

4. The next step is to query the specified student information (lookup ()).

The list subscript starts with 0, so "-1" is added after the input student number here


# Print information 
def lookup():
    sid = int(input(" Enter the enquiry student number :"))-1 # Student number 
    if sid > len(student1)-1:
        print(" Enter error, return to menu ")
    else:
        print(student1[sid]) # Output information 
    input(" Press Enter to continue ")

5. The next step is to add student information (append ())

Because the list elements are stored in a dictionary, the addition elements of the dictionary are used here


# Add information 
def append():
    newsid = eval(input(' Enter the new student number ( From 4 Begin ) : '))
    newname = eval(input(' Enter the name of a new student :'))
    newage = eval(input(' Enter the age of new students: '))
    newsocer = eval(input(' Enter new student grades: '))
    newstudent1 = {}
    newstudent1[newsid] = newname # Add   Student number and name 
    newstudent1['age'] = newage # Adding age 
    newstudent1['socer'] = newsocer # Add grades 
    student1.append(newstudent1) # Join list 
    input(" Press Enter to continue ")

6. If you can add it, you can delete it (delete ())


# Delete information 
def delete():
    did = int(input(" Enter the student number corresponding to the grade to be deleted: "))-1
    del student1[did] # Delete specified information 
    input(" Press Enter to continue ")

7, and then output all the information (allinformation ())

Here, the function str. format (), which formats strings, is used when traversing the output list elements


## Output all student information 
def allinformation():
    print(" All the information is as follows: ")
    i = 1
    for show in student1:
        print("{}".format(show)) # Print student information 
        i += 1
    input(" Press Enter to continue ")

8. Now you still need to modify the information (revise ())

It is also a method for dictionary to modify values, and the corresponding values are modified by keys


## Modify information 
def revise():
    resid = int(input(" Enter the student number to modify: "))-1    
    r = input(" Object to modify (1. Age; 2. Achievement ; Otherwise, all are modified ) : ")
    if r == '1':
        newage = input(" Revised age ")
        student1[resid]['age'] = newage
    elif r == '2':
        newsocer = input(" Revised results ")
        student1[resid]['socer'] = newsocer
    else:
        newage = input(" Revised age ")
        newsocer = input(" Revised results ")
        student1[resid]['socer'] = newsocer
        student1[resid]['age'] = newage
    input(" Press Enter to continue ")

9. I almost forgot to save 1 message... (save ())

I saved it in the student. txt file in the same file as the code, and the absolute path is required when introducing the path


## Save student information 
def save():
    f = open('student.txt','w') # Open a file 
    f.write(str(student1)) # Write to a file 
    f.close() # Close a file 
    input(" Save successfully and press Enter to continue ")

Then, the complete code is as follows:


'''
Created on 2021年6月13日
学生信息系统
列表存储学生信息,通过字典――键进行学生信息访问
@author: 小天
'''
 
student1 = [\
            {1:"jam",'age':17,'socer':99},
            {2:'kol','age':18,'socer':89},
            {3:'avlir','age':19,'socer':100}]
#页面
def menu():
    print('-'*30)
    print("欢迎来到学生管理系统")
    print("1.输入学号查询成绩")
    print("2.添加学生信息")
    print("3.删除学生信息")
    print("4.打印所有学生信息")
    print("5.修改学生信息")
    print("6.保存学生信息到指定文件")
    print("0.退出系统")
    print("-"*30)
#打印信息
def lookup():
    sid = int(input("输入查询学号:"))-1 #学号
    if sid > len(student1)-1:
        print("输入错误,返回菜单")
    else:
        print(student1[sid]) #输出信息
    input("按回车键继续")
#添加信息
def append():
    newsid = eval(input('输入新同学学号(从4开始):'))
    newname = eval(input('输入新同学姓名:'))
    newage = eval(input('输入新同学年龄:'))
    newsocer = eval(input('输入新同学成绩:'))
    newstudent1 = {}
    newstudent1[newsid] = newname #添加 学号、姓名
    newstudent1['age'] = newage #添加年龄
    newstudent1['socer'] = newsocer #添加成绩
    student1.append(newstudent1) #加入列表
    input("按回车键继续")
 
#删除信息
def delete():
    did = int(input("输入需要删除的成绩对应的学号:"))-1
    del student1[did] #删除指定信息
    input("按回车键继续")
 
##输出所有学生信息
def allinformation():
    print("所有信息如下:")
    i = 1
    for show in student1:
        print("{}".format(show)) #打印学生信息
        i += 1
    input("按回车键继续")
 
##修改信息
def revise():
    resid = int(input("输入要修改的学生学号:"))-1    
    r = input("要修改的对象(1.年龄;2.成绩;否则,都修改):")
    if r == '1':
        newage = input("修改后的年龄")
        student1[resid]['age'] = newage
    elif r == '2':
        newsocer = input("修改后的成绩")
        student1[resid]['socer'] = newsocer
    else:
        newage = input("修改后的年龄")
        newsocer = input("修改后的成绩")
        student1[resid]['socer'] = newsocer
        student1[resid]['age'] = newage
    input("按回车键继续")
    
##保存学生信息
def save():
    f = open('student.txt','w') #打开文件
    f.write(str(student1)) #写入文件
    f.close() #关闭文件
    input("保存成功按回车键继续")
    
if __name__ == '__main__':
    while True:
        menu()
        key = input("输入选项数字 查询/输入 信息:")
        if key == '1':
            lookup() #打印指定学生信息
        elif key == '2':
            append() #添加新的学生信息
        elif key == '3':
            delete() #删除指定学生信息
        elif key == '4':
            allinformation() #打印所有信息
        elif key == '5':
            revise() #修改指定学生信息
        elif key == '6':
            save() #保存信息
        elif key == '0':
            print("确定退出?")
            exit = input("输入yes退出:")
            if exit == 'yes':
                break
            else:
                print("输入错误,返回 ")

Output sample:

------------------------------
Welcome to Student Management System
1. Enter the student number to check the results
Step 2 Add student information
3. Delete student information
Step 4 Print all student information
STEP 5 Modify student information
6. Save student information to specified files
0. Exit the system
------------------------------
Input Options Numeric Query/Input Information: 2
Enter the new student number (starting from 4): 4
Enter new student name: 'dfg'
Enter the new student age: 17
Enter a new student's score: 78
Press Enter to continue
------------------------------
Welcome to Student Management System
1. Enter the student number to check the results
Step 2 Add student information
3. Delete student information
Step 4 Print all student information
STEP 5 Modify student information
6. Save student information to specified files
0. Exit the system
------------------------------
Input Options Numeric Query/Input Information: 6
Press Enter to continue
------------------------------
Welcome to Student Management System
1. Enter the student number to check the results
Step 2 Add student information
3. Delete student information
Step 4 Print all student information
STEP 5 Modify student information
6. Save student information to specified files
0. Exit the system
------------------------------
Input Options Numeric Query/Input Information: 4
All the information is as follows:
{1: 'jam', 'age': 17, 'socer': 99}
{2: 'kol', 'age': 18, 'socer': 89}
{3: 'avlir', 'age': 19, 'socer': 100}
{4: 'dfg', 'age': 17, 'socer': 78}
Press Enter to continue
------------------------------
Welcome to Student Management System
1. Enter the student number to check the results
Step 2 Add student information
3. Delete student information
Step 4 Print all student information
STEP 5 Modify student information
6. Save student information to specified files
0. Exit the system
------------------------------
Input Options Numeric Query/Input Information: 3
Enter the student number corresponding to the grade to be deleted: 4
Press Enter to continue
------------------------------
Welcome to Student Management System
1. Enter the student number to check the results
Step 2 Add student information
3. Delete student information
Step 4 Print all student information
STEP 5 Modify student information
6. Save student information to specified files
0. Exit the system
------------------------------
Input Options Numeric Query/Input Information: 6
Press Enter to continue
------------------------------
Welcome to Student Management System
1. Enter the student number to check the results
Step 2 Add student information
3. Delete student information
Step 4 Print all student information
STEP 5 Modify student information
6. Save student information to specified files
0. Exit the system
------------------------------
Input Options Numeric Query/Input Information: 0
Are you sure to quit?
Enter yes Exit: yes


Related articles: