基于python的-学生信息管理系统(升级版)(可存入本地.txt中)

'''
    1.添加学员
    2.修改学员
    3.查询学员
    4.删除学员
    0.退出程序
'''


import os

# 第一种保存方式
def save_data():
    # 1.打开文件
    file_handle = open('imformation.txt', 'w')
    # 2. for循环遍历大列表
    for student in stu_list:
        # 把列表中的数据用空格分开并拼接为一个字符串
        s = ' '.join(student)
        # 写入
        file_handle.write(s)
        file_handle.write('\n')

# 第二种保存方式
def save():
    file_handle = open('imformation.txt', 'w')
    for student in stu_list:
        for people in student:
            file_handle.write(people)
            file_handle.write(' ')
        file_handle.write('\n')
    file_handle.close()


def read():
    rs = os.path.exists('imformation.txt')
    if rs == True:
        file_handle = open('imformation.txt', 'r')
        contents = file_handle.readlines()
        for people in contents:
            people = people.strip('\n')
            list_1 = people.split(' ')
            list_1.pop()
            stu_list.append(list_1)
        file_handle.close()


def add():
    name = input('请输入要添加的学员姓名:')
    age = input('请输入要添加的学员年龄:')
    sex = input('请输入要添加的学员性别:')
    phone = input('请输入要添加的学员电话号码:')
    stu_list.append([name, age, sex, phone])
    save()
    print('----------------------------')
    print('添加成功!!!')


def change():
    if len(stu_list) == 0:
        print('----------------------------')
        print('没有学员信息,无法修改!')
        return  # 强制结束函数执行,return下所有代码都不会执行
    find_3()
    put = int(input('请输入您要修改的学员编号:'))
    while put not in range(0, len(stu_list)):
        put = int(input('编号不存在,请重新输入:'))
    name = stu_list[put]
    age = stu_list[put]
    sex = stu_list[put]
    phone = stu_list[put]
    new_name = input('请输入您修改后的姓名:')
    new_age = input('请输入您修改后的年龄:')
    new_sex = input('请输入您修改后的性别:')
    new_phone = input('请输入您修改后的电话号码:')
    stu_list[put] = [new_name, new_age, new_sex, new_phone]
    save()
    print('----------------------------')
    print('修改成功!!!')


def find_1():
    print('---------学员信息列表---------')
    if (len(stu_list) == 0):
        print('   (列表为空,无法查询!)   ')
        return
    put = int(input('请输入您要查询学生的编号:'))
    while put not in range(0, len(stu_list)):
        put = int(input('编号不存在,请重新输入:'))
    find_list = stu_list[put]
    name = find_list[0]
    age = find_list[1]
    sex = find_list[2]
    phone = find_list[3]
    print('编号:%s,姓名:%s,年龄:%s,性别:%s,电话号码:%s' % (put, name, age, sex, phone))


def find_2():
    print('---------学员信息列表---------')
    if (len(stu_list) == 0):
        print('   (列表为空,无法查询!)   ')
        return
    while True:
        name1 = input('请输入您要查询学生的姓名:')
        find = False
        for put in range(0, len(stu_list)):
            find_list = stu_list[put]
            name = find_list[0]
            age = find_list[1]
            sex = find_list[2]
            phone = find_list[3]
            if name == name1:
                find = True
                print('编号:%s,姓名:%s,年龄:%s,性别:%s,电话号码:%s' % (put, name1, age, sex, phone))
        if find == False:
            print('姓名不存在,请重新输入姓名:')
        else:
            find == True
            break


def find_3():
    print('---------学员信息列表---------')
    if( len(stu_list) == 0):
        print('           ()           ')
    for put in range(0, len(stu_list)):
        find_list = stu_list[put]
        name = find_list[0]
        age = find_list[1]
        sex = find_list[2]
        phone = find_list[3]
        print('编号:%s,姓名:%s,年龄:%s,性别:%s,电话号码:%s' % (put, name, age, sex, phone))


def delete_num():
    if len(stu_list) == 0:
        print('----------------------------')
        print('没有学员信息,无法删除!')
        return
    put = int(input('请输入您要删除的学员编号:'))
    while put not in range(0, len(stu_list)):
        put = int(input('编号不存在,请重新输入:'))

    del stu_list[put]
    save()
    print('----------------------------')
    print('删除成功!!!')


def delete_name():
    while True:
        name2 = input('请输入您要删除学生的姓名:')
        find = False
        for put in range(0, len(stu_list)):
            find_list = stu_list[put]
            name = find_list[0]
            if name == name2:
                find = True
                del stu_list[put]
                save()
                print('----------------------------')
                print('删除成功!!')
        if find == False:
            print('姓名不存在,请重新输入姓名:')
        else:
            break


def delete_both():
    stu_list.clear()
    save()
    print('----------------------------')
    print('已删除所有学员信息!!!')


stu_list = []
read()


while True:
    print('----------------------------')
    print('1.添加学员')
    print('2.修改学员')
    print('3.查询学员')
    print('4.删除学员')
    print('0.退出程序')
    print('----------------------------')

    num = int(input('请选择您要进行的操作:'))
    while num not in range(0, 5):
        num = int(input('没有该选项,请重新输入:'))
    if num == 1:
        add()
    elif num == 2:
        change()
    elif num == 3:
        print('1.按编号查询学员')
        print('2.按姓名查询学员')
        print('3.查询所有学员')
        print('----------------------------')
        num1 = int(input('请选择您要查询的方式:'))
        while num1 not in range(1, 4):
            num1 = int(input('没有该选项,请重新输入:'))
        if num1 == 1:
            find_1()
        elif num1 == 2:
            find_2()
        elif num1 == 3:
            find_3()
    elif num == 4:
        print('1.按编号删除学员')
        print('2.按姓名删除学员')
        print('3.删除所有学员')
        print('----------------------------')
        num2 = int(input('请选择您要删除的方式:'))
        while num2 not in range(1, 4):
            num2 = int(input('没有该选项,请重新输入:'))
        if num2 == 1:
            delete_num()
        elif num2 == 2:
            delete_name()
        elif num2 == 3:
            sure = input('确定删除所有信息?y(确定)/n(取消)')
            if sure == 'y':
                delete_both()
            elif sure == 'n':
                print('删除操作已取消!')
            else:
                print('输入有误')
    else:
        print('谢谢使用,再见!')
        break
  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值