實驗專案簡介


學校現在需要實作一個電子考勤系統,考慮到你們班已經學過大資料應用開發語言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 csv
import time
student_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
主函式我就不給出了,有需要的可以自己撰寫一下,如果需要可以私信我或者在這里下載資料集和原始碼喲!!!
點擊下載!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
看看運行效果喲!




每文一語
創作的思路來源于生活中細微的品味,勿驕勿躁,才是王道
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240542.html
標籤:python
