1. 程式人生 > >一個Python編寫的小程式(學生資訊管理系統)

一個Python編寫的小程式(學生資訊管理系統)

def printInfo(StudentData,StudentName):
    """
    :param StudentData:
    :param StudentName:
    :return:
    """
    student = StudentData[StudentName]
    student_Info = """
    ----------------------
    name:   %s
    age:    %s
    sex:    %s
    no:     %s
    ----------------------
    """%(student[2],student[3],student[4],student[5])

    print(student_Info)

def createStudent(StudentData,username,password,name,age,sex,no):
    """
    :param StudentData:
    :param username:
    :param password:
    :param name:
    :param age:
    :param sex:
    :param no:
    :return:
    """
    student = [username,password,name,age,sex,no]
    StudentData.setdefault(student[0],student)
    f =  open('account','r+',encoding='utf8')
    f.seek(0)
    f.truncate()
    for st in StudentData:
        row = ','.join(StudentData[st])
        f.write('%s\n'%row)
    f.flush()
    f.close()

def saveFile(StudentData):
    """

    :param StudentData:
    :return:
    """
    f = open('account', 'r+', encoding='utf8')
    f.seek(0)
    f.truncate()
    for st in StudentData:
        row = ','.join(StudentData[st])
        f.write('%s\n' % row)
    f.flush()
    f.close()

def readFile():
    """
    :return: accouts 從檔案將資料提出並轉化為dict
    """
    f = open('account','r',encoding='utf8')
    data = f.readlines()
    accounts = {}
    for line in data:
        line = line.strip()
        if not line.startswith('#'):
            items = line.split(",")
            accounts[items[0]] = items
    f.close()
    return accounts

def login(StudentData,account,password):
    """
    :param StudentData:
    :param account:
    :param password:
    :return:
    """
    if account in StudentData.keys():
        if password == StudentData[account][1]:
            return 1
        else:
            print("密碼輸入錯誤!")
            return None
    else:
        print("不存在該使用者!")
        return None

def changeInfo(StudentData,account):
    columnData = ["Name  Age   Sex   No"]
    print(columnData)
    for index, i in enumerate(StudentData[account]):
        if index>1:
            print("%s. %s"%(index-1,i))


if __name__ == "__main__":
    menu = """
        0.更換賬戶
        1.檢視資訊
        2.修改資訊
        3.修改密碼 
        4.建立使用者(需要管理員密碼)
           
    """
    StudentData = readFile()
    num = 0
    while num < 3:
        account = input("account:")
        password = input("password:")
        num+=1
        while login(StudentData,account,password):
            print("welcome %s ".center(50, '-') % account)
            print(menu)
            user_choice = input('>>>').strip()
            if user_choice.isdigit():
                user_choice = int(user_choice)
                if user_choice == 1:
                    printInfo(StudentData, account)
                if user_choice == 2:
                    changeInfo(StudentData, account)
                    ch = input('>>>').strip()
                    if ch.isdigit():
                        ch = int(ch)
                        for i in range(5):
                            if ch == i:
                                print("old:%s"%(StudentData[account][ch+1]))
                                new = input("new:").strip()
                                StudentData[account][ch+1]=new
                                saveFile(StudentData)
                                break
                            else:
                                print("未輸入規定範圍內數字")
                    else:
                        print("你輸入的不是數字")
                if user_choice == 3:
                    old = input("請輸入舊密碼:").strip()
                    if old == StudentData[account][1]:
                        new = input("輸入新的密碼:").strip()
                        StudentData[account][1] = new
                        saveFile(StudentData)
                        print("修改成功!")
                    else:
                        print("密碼輸入錯誤!")
                if  user_choice == 4:
                    print("你正在進行管理員操作...")
                    admin = input("請輸入管理員密碼:").strip()
                    if admin == 'admin':
                        new_account = input('new_account:')
                        new_password = input('new_password:')
                        new_name = input('new_name:')
                        new_age =  input('new_age:')
                        new_sex = input('new_sex:')
                        new_no = input('new_no:')
                        createStudent(StudentData,new_account,new_password,new_name,new_age,new_sex,new_no)
                    else:
                        print("密碼輸入錯誤!")
                if user_choice == 0:
                    break
    print("你輸入錯誤超過3次。。。")