1. 程式人生 > >python簡單實現學生管理系統

python簡單實現學生管理系統

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Author:Du Fei
import os

#學號,姓名,年齡,性別,身高
allStudentsList=[]
#從檔案中讀取資料
def readFromFile(fileName):
    if not os.path.exists(fileName):# 如果檔案不存在,則新增一個空檔案
        f = open(fileName,"w")
        f.close()
    with open(fileName,"r",encoding="utf-8") as f:
        for onStr in f:
            oneList=onStr.strip("\n").split(",")
            oneList[0]=int(oneList[0])#學號
            oneList[1]=oneList[1]#姓名
            oneList[2]=int(oneList[2])#年齡
            oneList[3]=oneList[3]#性別
            oneList[4]=float(oneList[4])#身高
            allStudentsList.append(oneList)

#將資料寫入檔案
def writeToFile(fileName):
    f =open(fileName,"w")
    f.close()

    with open(fileName,"a",encoding="utf-8") as f:
        for oneList in allStudentsList:
            oneStr=str(str(oneList[0])+","+oneList[1]+","+str(oneList[2])+","+oneList[3]+","+str(oneList[4])+"\n")
            f.write(oneStr)

#顯示所有學生資訊
def allStudentsShow():
    for one in allStudentsList:
        print("學號:%d,姓名:%s,年齡:%d,性別:%s,身高:%f" % (one[0], one[1], one[2], one[3],one[4]))

#新增學生
def addNewStudent(newStuList):
    #check學號是否重複復
    for one in allStudentsList:
        if one[0] == newStuList[0]:
            return -1 #學號重複
        allStudentsList.append(newStuList)
        writeToFile("student.txt")
        return 1

#修改學生年齡
def updateStudentsAge(id,newAge):
    for one in allStudentsList:
        if one[0]==id:
            one[2]=newAge
            break
    else:
        return -1#找不到此學號
    writeToFile("student.txt")
    return 1

#刪除學生
def deleteStudent(id):
    index =0
    for one in allStudentsList:
        if one[0] ==id:
            del allStudentsList[index]
            break
        index +=1
    else:
        return -1#找不到要刪除的學號
    writeToFile("student.txt")
    return 1

#按照姓名查詢
def searchStudents(name):
    flag =0
    for one in allStudentsList:
        if one[1]==name:
            print("學號:%d,姓名:%s,年齡:%d,性別:%s,身高:%f" % (one[0], one[1], one[2], one[3], one[4]))
            flag=1
    if flag==0:
        return -1#查無此人
    return 1


def sortMe(oneList):
    return oneList[0]

#按序號排序
def orderById(flag):#flag:1升序,2降序
    if flag==1:#升序
        allStudentsList.sort(reverse=False,key=lambda oneList:oneList[0])
    else:
        allStudentsList.sort(reverse=True,key=sortMe)

#功能選單
def menuShow():
    print("**********************************")
    print("*1.檢視所有學生資訊****************")
    print("*2.新增學生************************")
    print("*3.修改學生************************")
    print("*4.刪除學生************************")
    print("*5.按姓名查詢**********************")
    print("*6.按學號排序**********************")
    print("*7.儲存***************************")
    print("*8.退出***************************")
    print("**********************************")


if __name__ == "__main__":
    #從檔案中讀取資料
    readFromFile("student.txt")
    # print(allStudentsList)
    while True:
        # 顯示主選單
        menuShow()
        select =int(input("請選擇功能選項:"))
        if select == 1:
            allStudentsShow()
        elif select == 2:
            while True:
                try:
                    id = int(input("請輸入學號:"))
                    name = input("請輸入姓名:")
                    age = int(input("請輸入年齡:"))
                    sex = input("請輸入性別:")
                    height = float(input("請輸入身高:"))

                    newStuList=[id,name,age,sex,height]
                    if addNewStudent(newStuList) ==-1:
                        print("學號已存在,請重新輸入")
                    else:
                        flag=input("新增使用者成功,是否繼續新增(y/n)?:")
                        if flag.lower() !="y":
                            break
                except:
                    print("輸入有誤請重新輸入")

        elif select == 3:
            while True:
                id=int(input("請輸入序號:"))
                newAge=int(input("請輸入新的年齡:"))
                if updateStudentsAge(id,newAge) ==-1:
                    print("找不到此學號的學生,請重新輸入")
                else:
                    flag = input("修改成功,是否繼續修改(y/n)?:")
                    if flag.lower() != "y":
                        break

        elif select == 4:
            while True:
                id =int(input("請輸入刪除的學號:"))
                if deleteStudent(id) == -1:
                    print("找不到此學號的學生,請重新輸入")
                else:
                    flag = input("刪除成功,是否繼續刪除(y/n)?:")
                    if flag.lower() != "y":
                        break
        elif select == 5:
            while True:
                name=input("請輸入查詢的姓名:")
                if searchStudents(name) ==-1:
                    print("查無此人")

                flag = input("是否繼續查詢(y/n)?:")
                if flag.lower() != "y":
                    break

        elif select == 6:
            flag=int(input("請選擇排序方式(1:升序,2:降序)"))
            orderById(flag)
            allStudentsShow()
        elif select == 7:
            writeToFile("student.txt")
        else:
            exit()