溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用python實現(xiàn)學生信息管理系統(tǒng)

發(fā)布時間:2021-06-22 15:24:54 來源:億速云 閱讀:192 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么用python實現(xiàn)學生信息管理系統(tǒng)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

python實現(xiàn)學生信息管理系統(tǒng),供大家參考,具體內(nèi)容如下

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import os


# 主函數(shù)
def main():
    ctrl = True
    while (ctrl):
        menu()
        option = input("請選擇:")
        option_str = re.sub("\D", "", option)
        if option_str in ['0', '1', '2', '3', '4', '5', '6', '7']:
            option_int = int(option_str)
            if option_int == 0:
                print("您已退出學生信息管理系統(tǒng)!")
                ctrl = False
            elif option_int == 1:
                insert()
            elif option_int == 2:
                search()
            elif option_int == 3:
                delete()
            elif option_int == 4:
                modify()
            elif option_int == 5:
                sort()
            elif option_int == 6:
                total()
            elif option_int == 7:
                show()


# 顯示主菜單
def menu():
    print("""
    -------------------學生信息管理系統(tǒng)---------------------
    ======================功能菜單=========================
    
    1 錄入學生信息
    2 查找學生信息
    3 刪除學生信息
    4 修改學生信息
    5 排序
    6 統(tǒng)計學生總?cè)藬?shù)
    7 顯示所有學生信息
    0 退出系統(tǒng)
    ======================================================
    說明:通過數(shù)字或上下方向鍵選擇菜單
    ------------------------------------------------------
    """)


# 向指定文件寫入指定內(nèi)容的函數(shù)
filename = "students.txt"


def save(student):
    try:
        students_txt = open(filename, "a")
    except:
        students_txt = open(filename, "w")
    for info in student:
        students_txt.write(str(info) + "\n")
    students_txt.close()


#     1 錄入學生信息
def insert():
    studentList = []
    mark = True
    while mark:
        id = input("請輸入ID(如1001):")
        if not id:
            break
        name = input("請輸入名字:")
        if not name:
            break
        try:
            english = int(input("請輸入英語成績:"))
            python = int(input("請輸入Python成績:"))
            c = int(input("請輸入C語言成績:"))
        except:
            print("輸入無效,不是整型數(shù)值....重新錄入信息")
            continue
        # 信息保存到字典
        student = {"id": id, "name": name, "english": english, "python": python, "C語言": c}
        studentList.append(student)
        inputMark = input("是否繼續(xù)添加?(y/n):")
        if inputMark == "y":
            mark = True
        else:
            mark = False
        save(studentList)
        print("學生信息錄入完畢!")


#     2 查找學生信息
def search():
    mark = True
    student_query = []
    while mark:
        id = ""
        name = ""
        if os.path.exists(filename):
            mode = input("按ID查輸入1,按姓名查輸入2:")
            if mode == "1":
                id = input("請輸入學生ID:")
            elif mode == "2":
                name = input("請輸入學生姓名:")
            else:
                print("您的輸入有誤,請重新輸入!")
                search()
            with open(filename, 'r') as file:
                student = file.readlines()
                for list in student:
                    d = dict(eval(list))
                    if id is not "":
                        if d['id'] == id:
                            student_query.append(d)
                    elif name is not "":
                        if d['name'] == name:
                            student_query.append(d)
                show_student(student_query)
                student_query.clear()
                inputMark = input("是否繼續(xù)查詢?(y/n):")
                if inputMark == "y":
                    mark = True
                else:
                    mark = False
        else:
            print("暫未保存數(shù)據(jù)信息...")
            return


def show_student(studentList):
    if not studentList:
        print("無數(shù)據(jù)信息")
        return
    format_title = "{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}"
    print(format_title.format("ID", "名字", "英語成績", "Python成績", "C語言成績", "總成績"))
    format_data = "{:^6}{:^12}\t{:^12}\t{:^12}\t{:^12}\t{:^12}"
    for info in studentList:
        print(format_data.format(info.get("id"), info.get("name"), str(info.get("english")), str(info.get("python")),
                                 str(info.get("C語言")),
                                 str(info.get("english")+info.get("python")+info.get("C語言")).center(12)))


#     3 刪除學生信息
def delete():
    mark = True
    while mark:
        studentId = input("請輸入要刪除的學生ID:")
        if studentId is not "":
            if os.path.exists(filename):
                with open(filename, 'r') as rfile:
                    student_old = rfile.readlines()
            else:
                student_old = []
            ifdel = False
            if student_old:
                with open(filename, 'w') as wfile:
                    d = {}
                    for list in student_old:
                        d = dict(eval(list))
                        if d['id'] != studentId:
                            wfile.write(str(d) + "\n")
                        else:
                            ifdel = True
                    if ifdel:
                        print("ID為%s的學生信息已經(jīng)被刪除..." % studentId)
                    else:
                        print("沒有找到ID為%s的學生信息..." % studentId)
            else:
                print("無學生信息...")
                break
            show()
            inputMark = input("是否繼續(xù)刪除?(y/n):")
            if inputMark == "y":
                mark = True
            else:
                mark = False


#     4 修改學生信息
def modify():
    show()
    if os.path.exists(filename):
        with open(filename, 'r') as rfile:
            student_old = rfile.readlines()
    else:
        return
    studentid = input("請輸入要修改的學生ID:")
    with open(filename, "w") as wfile:
        for student in student_old:
            d = dict(eval(student))
            if d["id"] == studentid:
                print("找到了這名學生,可以修改他的信息!")
                while True:
                    try:
                        d["name"] = input("請輸入姓名:")
                        d["english"] = int(input("請輸入英語成績:"))
                        d["python"] = int(input("請輸入Python成績:"))
                        d["C語言"] = int(input("請輸入C語言成績:"))
                    except:
                        print("輸入信息有誤,重新輸入")
                    else:
                        break
                student = str(d)
                wfile.write(student + "\n")
                print("修改成功!")
            else:
                wfile.write(student)
    mark = input("是否繼續(xù)修改其他學生信息?(y/n):")
    if mark == "y":
        modify()


#     5 排序
def sort():
    show()
    if os.path.exists(filename):
        with open(filename, 'r') as file:
            student_old = file.readlines()
            student_new = []
        for list in student_old:
            d = dict(eval(list))
            student_new.append(d)
    else:
        return
    ascOrDesc = input("請選擇(0升序;1降序):")
    if ascOrDesc == "0":
        ascOrDescBool = False
    elif ascOrDesc == "1":
        ascOrDescBool = True
    else:
        print("您的輸入有誤,請重新輸入!")
        sort()
    mode = input("請選擇排序方式(1按英語排序;2按Python排序;3按C語言排序;0按總成績排序)")
    if mode == "1":
        student_new.sort(key=lambda x: x["english"], reverse=ascOrDescBool)
    elif mode == "2":
        student_new.sort(key=lambda x: x["python"], reverse=ascOrDescBool)
    elif mode == "3":
        student_new.sort(key=lambda x: x["C語言"], reverse=ascOrDescBool)
    elif mode == "0":
        student_new.sort(key=lambda x: x["english"] + x["python"] + x["C語言"], reverse=ascOrDescBool)
    else:
        print("您的輸入有誤,請重新輸入!")
        sort()
    show_student(student_new)


#     6 統(tǒng)計學生總?cè)藬?shù)
def total():
    if os.path.exists(filename):
        with open(filename, 'r') as rfile:
            student_old = rfile.readlines()
            if student_old:
                print("一共有%s名學生" % len(student_old))
            else:
                print("還沒有錄入學生信息!")
    else:
        print("暫未保存數(shù)據(jù)信息...")


#     7 顯示所有學生信息
def show():
    student_new = []
    if os.path.exists(filename):
        with open(filename, 'r') as rfile:
            student_old = rfile.readlines()
        for list in student_old:
            student_new.append(eval(list))
        if student_new:
            show_student(student_new)
    else:
        print("暫未保存數(shù)據(jù)信息...")


#     0 退出系統(tǒng)


if __name__ == '__main__':
    main()

安裝pyinstaller打包成可執(zhí)行exe文件

pip install pyinstaller
...

(pydemo) D:\JavaProjects\PythonProject\01學生信息管理系統(tǒng)>pyinstaller --version
4.3
(pydemo) D:\JavaProjects\PythonProject\01學生信息管理系統(tǒng)>pyinstaller -F D:\JavaProjects\PythonProject\01學生信息管理系統(tǒng)\StuManagerSys.py

在下面的路徑即可找到打包后的exe文件

怎么用python實現(xiàn)學生信息管理系統(tǒng)

“怎么用python實現(xiàn)學生信息管理系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI