import csv
import time
class studentManage:
def __init__(self):
self.student_infos = []
self.student = []
def login(self):
"""
用用学号和密码进行登录
最多让用户登录三次,如果连续三次都登录失败(用户名或者密码错误),只要密码和用户都正确表示登录成功
:return:登录成功返回True和姓名,三次都登录失败返回False和None
"""
retry_time = 0
while retry_time < 3:
name = input('请输入登录姓名::')
account = input('请输入学号:')
for i in self.student_infos:
# 判断姓名学号是否等于文件中的数据
if i['姓名'] == name and i['学号'] == account:
return True, account
retry_time += 1
print('用户名或者密码错误,你还有' + str(3 - retry_time) + '次机会!!!请重新输入。')
else:
return False, None
# 加载学生数据
def load_stu_info(self):
"""
加载学生信息
从studentMsg.csv文件中加载数据
:return: 无
"""
with open(r"../src/studentMsg.csv", encoding='ANSI', errors="ignore") 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]
# append 添加学生信息
self.student_infos.append(student_info)
# 添加数据
def add(self, user_no):
# 在数据列表中通过学号找查学生
for x in self.student_infos:
if user_no == x['学号']:
name = x['姓名']
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]
# 写入csv文件
with open(r"../src/studentMsg.csv", 'a+', newline='', encoding='ANSI') as f:
wf = csv.writer(f)
wf.writerow([name, user_no, times, data]) # 写入一行数据
print("{}同学{}数据已经写入成功!操作时间是{}".format(name, data, times))
# 找查学生
def select(self):
# 读取csv文件
with open(r"../src/studentMsg.csv", encoding='ANSI') as file:
f_csv = csv.reader(file)
# 获取表头
header = next(f_csv)
# 循环每一行的数据
for row in f_csv:
students = {}
# 查询所有的数据 索引0,1,2,3
for index in range(4):
students[header[index]] = row[index]
# 把查询到的所有数据插入表中
self.student.append(students)
name = str(input("请输入你需要查找的姓名:"))
print(" 姓名\t\t学号\t\t操作时间\t\t出勤状态")
# 是否查到的判断
isData = True
# 循环找查姓名==name的同学数据
for a in self.student:
if a['姓名'] == name:
isData = False
print(a['姓名'] + '\t' + a['学号'] + '\t' + a['时间'] + '\t\t' + a['状态'])
if isData:
print("无此人")
python 学生考勤系统
最新推荐文章于 2024-09-17 12:03:03 发布