day11 - 作業(yè)(學(xué)生系統(tǒng))

#_*_encoding=utf-8_*_
def interface():
    """
    歡迎界面
    :return: 無
    """
    print('='.center(40, '='))
    print("歡迎ZH:")
    print(' '*15 + "1. 添加學(xué)生")
    print(' '*15 + "2. 查看學(xué)生")
    print(' '*15 + "3. 修改學(xué)生信息")
    print(' '*15 + "4. 刪除學(xué)生")
    print(' '*15 + "5. 返回")
    print('='.center(40, '='))


def add_stu():
    """
    添加學(xué)生信息
    :return: 無
    """
    stu_file = open("D:\\學(xué)生.txt", 'a+')
    while True:
        print('='.center(40, '='))
        stu_name = input("請輸入學(xué)生姓名:")
        stu_age = int(input("請輸入學(xué)生的年齡:"))
        stu_tel = input("請輸入學(xué)生的電話:")
        stu_file.write('姓名:' + stu_name + ' 年齡:%d' % stu_age + ' 電話:' + stu_tel + '\n')
        print("添加成功!")
        print('='.center(40, '='))
        while True:
            print("1.繼續(xù)\n2.返回")
            n = input("請輸入選擇(1-2):")
            if n == '1':
                break
            if n == '2':
                stu_file.close()
                return
            print("輸入錯誤!")


def view_stu():
    """
    查看學(xué)生信息
    :return: 無
    """
    stu_file = open("D:\\學(xué)生.txt")
    while True:
        print('='.center(40, '='))
        stu_name = input("請輸入查看學(xué)生的姓名:")
        flag = 0
        for stu_str in stu_file.readlines():
            name_str = ''
            for ch in stu_str[3:]:
                if ch == ' ':
                    break
                name_str += ch
            if name_str == stu_name:
                print(stu_str[:-2])
                flag = 1
                break
        if flag == 0:
            print("很遺憾,未找到該學(xué)生信息!")
        print('='.center(40, '='))
        while True:
            print("1.繼續(xù)\n2.返回")
            n = input("請輸入選擇(1-2):")
            if n == '1':
                stu_file.seek(0)
                break
            if n == '2':
                stu_file.close()
                return
            print("輸入錯誤!")


def modify_stu():
    """
    修改學(xué)生信息
    :return: 無
    """
    while True:
        print('='.center(40, '='))
        stu_name = input("請輸入修改學(xué)生的姓名:")
        flag = 0
        file_data = ""
        with open("D:\\學(xué)生.txt", 'r') as stu_file:
            for stu_str in stu_file.readlines():
                name_str = ''
                for ch in stu_str[3:]:
                    if ch == ' ':
                        break
                    name_str += ch
                if name_str == stu_name:
                    stu_age = int(input("請輸入修改學(xué)生的年齡:"))
                    stu_tel = input("請輸入修改學(xué)生的電話:")
                    stu_str = '姓名:' + stu_name + ' 年齡:%d' % stu_age + ' 電話:' + stu_tel + '\n'
                    flag = 1
                file_data += stu_str
        with open("D:\\學(xué)生.txt", 'w') as stu_file:
            stu_file.write(file_data)
        if flag == 0:
            print("很遺憾,未找到該學(xué)生信息!")
        if flag == 1:
            print("修改成功!")
        print('='.center(40, '='))
        while True:
            print("1.繼續(xù)\n2.返回")
            n = input("請輸入選擇(1-2):")
            if n == '1':
                break
            if n == '2':
                return
            print("輸入錯誤!")


def remove_stu():
    """
    刪除學(xué)生信息
    :return: 無
    """
    while True:
        print('='.center(40, '='))
        stu_name = input("請輸入刪除學(xué)生的姓名:")
        flag = 0
        file_data = ""
        with open("D:\\學(xué)生.txt", 'r') as stu_file:
            for stu_str in stu_file.readlines():
                name_str = ''
                for ch in stu_str[3:]:
                    if ch == ' ':
                        break
                    name_str += ch
                if name_str == stu_name:
                    flag = 1
                    continue
                file_data += stu_str
        with open("D:\\學(xué)生.txt", 'w') as stu_file:
            stu_file.write(file_data)
        if flag == 0:
            print("很遺憾,未找到該學(xué)生信息!")
        if flag == 1:
            print("刪除成功!")
        print('='.center(40, '='))
        while True:
            print("1.繼續(xù)\n2.返回")
            n = input("請輸入選擇(1-2):")
            if n == '1':
                break
            if n == '2':
                return
            print("輸入錯誤!")


def stu_sys():
    """
    學(xué)生系統(tǒng)
    :return: 無
    """
    while True:
        interface()
        n = input("請輸入選擇(1-5):")
        while True:
            if n == '1':
                add_stu()
            if n == '2':
                view_stu()
            if n == '3':
                modify_stu()
            if n == '4':
                remove_stu()
            if n == '5':
                return
            if not (1 <= int(n) <= 5):
                print("輸入錯誤!")
                n = input("請輸入選擇(1-5):")
                continue
            interface()
            n = input("請輸入選擇(1-5):")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容