【Python基础】8.实现学籍管理系统

本文将通过建立学生学籍管理系统,总结python基础内容,并扩充细节知识点

项目演示

目标

  • 搭建学籍管理系统框架结构1
    • 准备工程
    • 编写主要运行循环,实现用户输入和判断
    • 编写工具模块,实现学籍管理系统的增删改查

stu_main.py 文件

while True: # 无限循环保证程序不退出
    print('Student Management System')
    print('--------------------------')
    print('1. Add student')
    print('2. Delete student')
    print('3. Modify student')
    print('4. Search student')
    print('5. Display all students')
    print('6. Exit')
    print('--------------------------')
    choice = int(input('Enter your choice: '))
    print("You entered: ", choice)
    if choice == 1:
        pass
    elif choice == 2:
       pass
    elif choice == 3:
        pass
    elif choice == 4:
        pass
    elif choice == 5:
        pass
    elif choice == 6:
        break
    else:
        print('Invalid choice')
        

pass 关键字

如果在开发程序时,不想立刻编写分支内部的代码。可以使用pass关键字,表示一个占位符,保证程序的结构正确,程序运行时,pass不会执行任何操作。

#TODO 代码注释

  • TODO 必须大写,方便查看要完成的代码块
  • 使用TODO 注释,配合IDE,可以快速找到未完成的代码块

Pycharm 重命名标识符

选中标识符,按Shift+F6,可以批量重命名

stu_tool.py 文件

students = []


def print_menu():
    '''
    Print the menu
    :return:
    '''
    print()
    print('Student Management System')
    print('--------------------------')
    print('1. Add student')
    print('2. Delete student')
    print('3. Modify student')
    print('4. Search student')
    print('5. Display all students')
    print('6. Exit')
    print('--------------------------')


def display_student(stu_no):
    '''
    Display student
    :param stu_no:
    :return:
    '''
    print('Student found')
    print("-" * 30)
    for stu_header in students[0]:
        print(stu_header.ljust(10), end=' ')
    print()
    for stu_header in students[0]:
        print(stu_header[stu_header].ljust(10), end=' ')
    print()


def add_student():
    '''
    Add student
    :return:
    '''
    print('Add student')
    # 输入学生学号
    stu_no = input('Enter student number: ')
    # 输入学生姓名
    name = input('Enter student name: ')
    # 输入学生年龄
    age = input('Enter student age:')
    # 输入学生邮箱
    email = input('Enter student email:')
    # 创建学生字典
    student = {'stu_no': stu_no, 'name': name, 'age': age, 'email': email}
    # 将学生字典添加到学生列表
    students.append(student)
    print('Student added successfully!')
    print("-" * 30)
    display_student(stu_no)


def delete_student():
    '''
    Delete student
    :return:
    '''
    # 如果学生列表不为空
    if not len(students) == 0:
        print('Delete student')
        print("-" * 30)
        # 输入学生学号
        stu_no = input('Enter student number: ')
        # 遍历学生列表
        for stu in students:
            # 如果学生学号等于输入学号
            if stu['stu_no'] == stu_no:
                print('Student found')
                print("-" * 30)
                for stu_header in students[0]:
                    print(stu_header.ljust(10), end=' ')
                print()
                for stu_header in students[0]:
                    print(stu[stu_header].ljust(10), end=' ')
                # 删除学生
                students.remove(stu)
                print('Student deleted successfully!')
                break
        else:
            print('Student not found')


def modify_student():
    # 如果学生列表不为空
    if not len(students) == 0:
        print('Modify student')
        print("-" * 30)
        # 输入学生学号
        stu_no = input('Enter student number: ')
        for stu in students:
            # 如果学生学号等于输入学号
            if stu['stu_no'] == stu_no:
                print('Student found')
                print("-" * 30)
                for stu_header in students[0]:
                    print(stu_header.ljust(10), end=' ')
                print()
                for stu_header in students[0]:
                    print(stu[stu_header].ljust(10), end=' ')
                print()
                # 输入学生姓名
                name = input('Enter student name: ')
                # 输入学生年龄
                age = input('Enter student age:')
                # 输入学生邮箱
                email = input('Enter student email:')
                # 创建学生字典
                student = {'stu_no': stu_no, 'name': name, 'age': age, 'email': email}
                students.remove(stu)
                students.append(student)
                print('Student modified successfully!')
                print("-" * 30)
                for stu_header in student:
                    print(stu_header.ljust(10), end=' ')
                for stu in students:
                    print()
                    for stu_header in student:
                        print(stu[stu_header].ljust(10), end=' ')
                break
        else:
            print('Student not found')


def search_student():
    if not len(students) == 0:
        print('Search student')
        print("-" * 30)
        stu_no = input('Enter student number: ')
        for stu in students:
            if stu['stu_no'] == stu_no:
                print('Student found')
                print("-" * 30)
                for stu_header in students[0]:
                    print(stu_header.ljust(10), end=' ')
                print()
                for stu_header in students[0]:
                    print(stu[stu_header].ljust(10), end=' ')
                break
        else:
            print('Student not found')


def display_students():
    if not len(students) == 0:
        print('Display all students')
        print("-" * 30)
        for stu_header in students[0]:
            print(stu_header.ljust(10), end=' ')
        for stu in students:
            print()
            for stu_header in students[0]:
                print(stu[stu_header].ljust(10), end=' ')

修改stu_main.py文件


import stu_tool

while True:
    stu_tool.print_menu()
    choice = input('Enter your choice: ')
    print("You entered: ", choice)
    if choice in ['1', '2', '3', '4', '5']:
        if choice == '1':
            stu_tool.add_student()
        elif choice == '2':
            stu_tool.delete_student()
        elif choice == '3':
            stu_tool.modify_student()
        elif choice == '4':
            stu_tool.search_student()
        elif choice == '5':
            stu_tool.display_students()
    elif choice == '6':
        print('Thank you for using the system!')
        break

    else:
        print('Invalid choice')

  1. 本文章代码内容均由Github Coplilot 补充实现 ↩︎

  • 16
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值