來源丨網絡
今天給大家分享一個比較有意思的Python應用,用 Python 寫了一個電子考勤系統,源碼已在文章全部給出,記得點讚收藏哦~項目簡介學校現在需要實現一個電子考勤系統,考慮到你們班已經學過大數據應用開發語言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文件,定義了數據加載、登錄等函數。
答題要求在stu_info.csv文件末尾添加一行自己的信息,密碼隨意寫,名字和學號必須是自己查看兩個Python文件中的todo注釋,添加合適代碼,最終提供添加的代碼。測試程序功能,提供程序運行截圖。進行登錄驗證的時候使用自己的學號進行登錄驗證,並且需要測試如下2個分支:3次都登錄失敗的情況、登錄成功後成功添加考勤數據。附加功能添加一個查詢功能,輸入一個學生的姓名就可以獲取他的出勤數據信息
導入模塊importcsvimporttimestudent_infos=[]加載數據defload_stu_info():"""加載學生信息從stu_infos.csv文件中加載數據:return:無"""withopen(r"stu_infos.csv",encoding='utf-8-sig')asfile:f_csv=csv.reader(file)header=next(f_csv)forrowinf_csv:student_info={}forindexinrange(3):student_info[header[index]]=row[index]student_infos.append(student_info)登錄deflogin():"""用戶使用學號和密碼進行登錄最多讓用戶登錄三次,如果連續三次都登錄失敗(用戶名或者密碼錯誤),只要密碼和用戶都正確表示登錄成功:return:登錄成功返回True和學號,三次都登錄失敗返回False和None"""retry_time=0whileretry_time<3:user_no=input('請輸入登錄賬號:')password=input('請輸入密碼:')foriinstudent_infos:ifi['no']==user_noandi['password']==password:returnTrue,user_noprint('用戶名或者密碼錯誤!!!請重新輸入。')retry_time+=1else:returnFalse,None考勤記錄寫入defadd(user_no):forxinstudent_infos:ifuser_no==x['no']:name=x['name']breaktimes=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())choices=['出勤','遲到','請假','缺勤']a=int(input("\t該學生出勤情況:1-出勤\t2-遲到\t3-請假\t4-缺勤:"))ifa==1:data=choices[0]elifa==2:data=choices[1]elifa==3:data=choices[2]else:data=choices[3]withopen(r"attendance.csv",'a+',newline='',encoding='utf-8')asf:wf=csv.writer(f)wf.writerow([user_no,name,times,data])#寫入一行數據print("{}同學{}數據已經寫入成功!操作時間是{}".format(name,data,times))查詢考勤記錄defselect():student=[]withopen(r"attendance.csv",encoding='utf-8-sig')asfile:f_csv=csv.reader(file)header=next(f_csv)forrowinf_csv:students={}forindexinrange(4):students[header[index]]=row[index]student.append(students)name=input("請輸入你需要查找的姓名:")print("學號\t\t姓名\t\t操作時間\t\t出勤狀態")forainstudent:ifa['name']==name:print(a['no']+'\t'+a['name']+'\t'+a['time']+'\t\t'+a['state'])else:print("無此人!!!")break主函數main.pyfromstudent.stu_attendanceimport*if__name__=='__main__':load_stu_info()success,stu_no=login()print(stu_no)ifsuccess:print('登錄成功!')add(stu_no)q=int(input("你想要查詢出勤數據嗎?\tyes(1)--no(0)"))ifq==1:select()else:print("歡迎下次登錄電子考勤系統")else:print('登錄失敗')看看運行效果喲!




來源:blog.csdn.net/weixin_47723732
·················END·················