學校現在需要實現一個電子考勤系統,考慮到你們班已經學過大資料應用開發語言Python,準備讓你們實現部分學生端考勤功能。經過和老師的溝通,你瞭解到: (1) 目前該系統已經被學長實現了部分功能,你們只需要完成剩餘功能即可,需要你們完成的功能會使用 #todo的形式進行標註, todo後面會列出這個地方的功能,形式如下。
(2) 學生資訊儲存在stu_infos.csv檔案中,第一行是列名行,後面每一行都是一個學生的資訊,包含學號,姓名,密碼。內容形式如下:
(3) 考勤記錄最終會被儲存到attendance.csv檔案中,第一行是列名行,後面每一行代表一個學生的考勤資訊,包含學號,姓名,時間,考勤狀態(只有出勤、遲到、請假、缺勤四種狀態)。內容格式如下:
(4) 學生資訊需要首先被載入到student_infos列表中,student_info中的每個元素都是一個字典,字典中的鍵都是各自列名,而值則是每一行內容,按照示例資料構造出來的student_infos列表如下。
(5) 考勤系統老師端總共有兩個Python檔案,一個main.py檔案,該檔案作為入口程式檔案,實現主體框架,主體流程就是:載入資料 登入 新增考勤資料;一個stu_attendance.py檔案,定義了資料載入、登入等函式。
答題要求: (1) 在stu_info.csv檔案末尾新增一行自己的資訊,密碼隨意寫,名字和學號必須是自己 (2) 檢視兩個Python檔案中的todo註釋,新增合適程式碼,最終提供新增的程式碼。 (3) 測試程式功能,提供程式執行截圖。進行登入驗證的時候使用自己的學號進行登入驗證,並且需要測試如下2個分支:3次都登入失敗的情況、登入成功後成功新增考勤資料。
附加功能
新增一個查詢功能,輸入一個學生的姓名就可以獲取他的出勤資料資訊
匯入模組
import csvimport timestudent_infos = []
載入資料
def load_stu_info(): """ 載入學生資訊 從stu_infos.csv檔案中載入資料 :return: 無 """ with open(r"stu_infos.csv", encoding='utf-8-sig') as file: f_csv = csv.reader(file) header = next(f_csv) for row in f_csv: student_info = {} for index in range(3): student_info[header[index]] = row[index] student_infos.append(student_info)
登入
def login(): """ 使用者使用學號和密碼進行登入 最多讓使用者登入三次,如果連續三次都登入失敗(使用者名稱或者密碼錯誤),只要密碼和使用者都正確表示登入成功 :return:登入成功返回True和學號,三次都登入失敗返回False和None """ retry_time = 0 while retry_time < 3: user_no = input('請輸入登入賬號:') password = input('請輸入密碼:') for i in student_infos: if i['no']==user_no and i['password']==password: return True,user_no print('使用者名稱或者密碼錯誤!!!請重新輸入。') retry_time += 1 else: return False, None
考勤記錄寫入
def add(user_no): for x in student_infos: if user_no==x['no']: name=x['name'] break times=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) choices=['出勤','遲到','請假','缺勤'] a=int(input("\t該學生出勤情況:1-出勤\t2-遲到\t3-請假\t4-缺勤:")) if a==1: data=choices[0] elif a==2: data=choices[1] elif a==3: data=choices[2] else: data=choices[3] with open(r"attendance.csv",'a+',newline='', encoding='utf-8') as f: wf = csv.writer(f) wf.writerow([user_no,name,times,data])#寫入一行資料 print("{}同學{}資料已經寫入成功!操作時間是{}".format(name,data,times))
查詢考勤記錄
def select(): student = [] with open(r"attendance.csv", encoding='utf-8-sig') as file: f_csv = csv.reader(file) header = next(f_csv) for row in f_csv: students = {} for index in range(4): students[header[index]] = row[index] student.append(students) name=input("請輸入你需要查詢的姓名:") print(" 學號\t\t姓名\t\t操作時間\t\t出勤狀態") for a in student: if a['name']==name: print(a['no']+'\t'+a['name']+'\t'+a['time']+'\t\t'+a['state']) else: print("無此人!!!") break
主函式我就不給出了,有需要的可以自己編寫一下,如果需要可以私信我或者在這裡下載資料集和原始碼喲!!!
看看執行效果喲!
每文一語