Python practice student management system (function version)

Use the basic knowledge of python to realize the function version of the student management system.

First we need to fulfill the following requirements:

  1. find students
  2. delete student information
  3. Add student information
  4. Modify student information
  5. Student information includes name, number of languages ​​and total score
  6. Output all student information
  7. Exit system

In order to realize the simple version of the student management system for the above requirements, we need to realize the operation of adding, deleting, modifying and checking, then we need to enter the corresponding operation to realize the corresponding requirements, such as inputting the number 3 to add students, and so on. Then we can use the flow control statements we have learned to operate. Before realizing the requirements, we also need to consider which form of data storage to use. Think about it, can the data container and list we learned earlier be able to add, delete, modify and check? At the same time, in order to better understand the data, we can use the form of nested dictionaries in the list such as [{},{},{}...], so that we can better realize our needs, let's start directly.

We can write out the backbone of the code first, and then implement other requirements step by step.

def run():
    total_student = []
    # 设置死循环,持续执行相应的操作
    while True:
        instr = input('请输入操作:')
        # 1.查找学生信息
        if instr == '1':
            pass
        # 2.删除学生信息
        elif instr == '2':
            pass
        # 3.添加学生信息
        elif instr == '3':
            pass
        # 4.修改学生信息
        elif instr == '4':
            pass
        # 5.打印全部学生信息
        elif instr == '5':
            pass
        # 0.结束操作
        elif instr == '0':
        	print('退出系统!')
        	# break 结束循环
            break
        else:
            print('错误操作!')


In order for people to better understand the corresponding operations, we can add a prompt function.

def tip():
    print('-----------帮助------------')
    print('     ', '1.查找学生信息')
    print('     ', '2.删除学生信息')
    print('     ', '3.添加学生信息')
    print('     ', '4.修改学生信息')
    print('     ', '5.打印全部学生信息')
    print('     ', '6.结束')
    print('-----------结束------------')

Next, let's implement the search for students. We can define a function to receive the incoming total student information, and then traverse one by one, stop if found, and prompt no such person if not found, use for to traverse.

def find_student(data):
    name = input('请输入学生姓名:')
    for i in data:
        if name == i.get('姓名'):
        # 返回含有学生信息的字典
            return i
    else:
        print('无此人!')
        # 返回空字符
        return ''

Let’s delete student information again. The prerequisite for deleting a student is to find the student and then delete it. If not, it will prompt that there is no. If we look carefully, we will find that we need to search for deleting student information and modifying student information, because we encapsulate the search function into A function, as long as it is called, is also a feature of the function, code reuse. So we just call it.

def del_student(data):
	# 返回的是字典
    student = find_student(data)
    if student:
        print(f'删除{student.get("姓名")}成功!')
        data.remove(student)

Adding student information is nothing more than adding a dictionary to the list, so let's implement it.

# 添加学生信息
def add_student(data):
	# 成绩必须是数字 否则会报错
    student_name = input('请输入学生姓名:')
    student_chine = input('请输入语文成绩:')
    student_math = input('请输入数学成绩:')
    student_english = input('请输入英语成绩:')
    total = int(student_english) + int(student_chine) + int(student_math)
    data.append({
    
    '姓名': student_name, '语文': int(student_chine), '数学': int(student_math), '英语': int(student_english),
                 '总分': total})
    print(f'添加{student_name}信息成功!')

Some student information will be wrong, but we don’t have to modify all of them. We can use the if statement to judge, enter the Enter key to not modify, and enter the modification statement to modify.

# 修改学生信息
def change_student(data):
    # 不修改按回车键
    stu1 = find_student(data)
    if stu1:
        student_name = input('请重新输入姓名:')
        if student_name:
            stu1['姓名'] = student_name
        student_chine = input('请输入语文成绩:')
        if student_chine:
            stu1['语文'] = int(student_chine)
        student_math = input('请输入数学成绩:')
        if student_math:
            stu1['数学'] = int(student_math)
        student_english = input('请输入英语成绩:')
        if student_english:
            stu1['英语'] = int(student_english)
        total = stu1.get('语文') + stu1.get('数学') + stu1.get('英语')
        stu1['总分'] = total
        print(f'修改{stu1.get("姓名")}学生成功!')

Let's realize the output of all student information, traverse and print, and it can be realized.

def print_total(data):
    # 列表为空
    if data:
        print('姓名', '\t语文', '\t数学', '\t英语', '\t总分')
        for s in data:
            print(s.get('姓名'), '\t\t', s.get('语文'), '\t\t', s.get('数学'), '\t\t', s.get('英语'), '\t\t', s.get('总分'),
                  sep='')
    else:
        print('-----------数据为空------------')

Finally, we put the code together. The complete code is as follows

def find_student(data):
    name = input('请输入学生姓名:')
    for i in data:
        if name == i.get('姓名'):
            return i
    else:
        print('无此人!')
        return ''


def del_student(data):
    student = find_student(data)
    if student:
        print(f'删除{student.get("姓名")}成功!')
        data.remove(student)


# 添加学生信息
def add_student(data):
    # 如果学生没有输入正确的成绩呢?不如应该是数字 结果输入的是str
    student_name = input('请输入学生姓名:')
    student_chine = input('请输入语文成绩:')
    student_math = input('请输入数学成绩:')
    student_english = input('请输入英语成绩:')
    total = int(student_english) + int(student_chine) + int(student_math)
    data.append({
    
    '姓名': student_name, '语文': int(student_chine), '数学': int(student_math), '英语': int(student_english),
                 '总分': total})
    print(f'添加{student_name}信息成功!')


# 修改学生信息
def change_student(data):
    # 不修改按回车键
    stu1 = find_student(data)
    if stu1:
        student_name = input('请重新输入姓名:')
        if student_name:
            stu1['姓名'] = student_name
        student_chine = input('请输入语文成绩:')
        if student_chine:
            stu1['语文'] = int(student_chine)
        student_math = input('请输入数学成绩:')
        if student_math:
            stu1['数学'] = int(student_math)
        student_english = input('请输入英语成绩:')
        if student_english:
            stu1['英语'] = int(student_english)
        total = stu1.get('语文') + stu1.get('数学') + stu1.get('英语')
        stu1['总分'] = total
        print(f'修改{stu1.get("姓名")}学生成功!')


def print_total(data):
    # 列表为空
    if data:
        print('姓名', '\t语文', '\t数学', '\t英语', '\t总分')
        for s in data:
            print(s.get('姓名'), '\t\t', s.get('语文'), '\t\t', s.get('数学'), '\t\t', s.get('英语'), '\t\t', s.get('总分'),
                  sep='')
    else:
        print('-----------数据为空------------')


def tip():
    print('-----------帮助------------')
    print('     ', '1.查找学生信息')
    print('     ', '2.删除学生信息')
    print('     ', '3.添加学生信息')
    print('     ', '4.修改学生信息')
    print('     ', '5.输出全部学生信息')
    print('     ', '0.退出系统')
    print('-----------结束------------')


def run():
    total_student = [{
    
    '姓名': '天天', '语文': 98, '数学': 99, '英语': 99, '总分': 296}]
    # 设置死循环,一直执行相应的操作
    while True:
        tip()
        instr = input('请输入操作:')
        # 1.查找学生信息
        if instr == '1':
            print('-----------查找学生信息------------')
            stu = find_student(total_student)
            if stu:
                # 对齐 整洁
                print('姓名', '\t语文', '\t数学', '\t英语', '\t总分')
                print(stu.get('姓名'), '\t\t', stu.get('语文'), '\t\t', stu.get('数学'), '\t\t', stu.get('英语'), '\t\t',
                      stu.get('总分'), sep='')
        # 2.删除学生信息
        elif instr == '2':
            print('-----------删除学生信息------------')
            del_student(total_student)
        # 3.添加学生信息
        elif instr == '3':
            print('-----------添加学生信息------------')
            add_student(total_student)
        # 4.修改学生信息
        elif instr == '4':
            print('-----------修改学生信息------------')
            change_student(total_student)
        # 5.打印全部学生信息
        elif instr == '5':
            print('-----------全部学生信息------------')
            print_total(total_student)
        # 0.结束操作
        elif instr == '0':
            print('-----------结束操作------------')
            print('退出系统!')
            break
        else:
            print('错误操作!')


if __name__ == '__main__':
    run()

This simple student management system is very basic and great for practice. In fact, this system is not very perfect, there are many places that need to be improved. For example, the data cannot be persisted, the program ends, and the data is gone. You can use python file operations to save the data locally. You can read my article about file and directory operations. When there are a lot of students, can we sort by a certain keyword? Such as total score, other grades, etc.

Guess you like

Origin blog.csdn.net/qq_65898266/article/details/124975500