# auth marcuya
'''
作业:编写登陆模块
1.输入用户名和密码
2.认证成功显示欢迎信息
3.输错3次后锁定用户
设计思路:
1.用户信息保存文件有5个字段:序号,用户名,密码,密码输入错误次数,是否锁定
2.密码输入错误次数大于2,锁定字段为TRUE,用户不能登陆'''
import openpyxl
while True:
#获取用户输入的用户名和密码
print()
_username = input("please input username:")
_password = input("please input password:")
# 打开文件
userInfoFile = openpyxl.load_workbook("homeWork1CountInfo.xlsx")
userInfoSheet = userInfoFile.active
#errorcount = userInfoSheet.cell(row=2, column=4)
#print(errorcount.value)
#errorcount.value = 3
#userInfoFile.save("homeWork1CountInfo.xlsx")
#逐行轮询
for rows in userInfoSheet.iter_rows():
no = rows[0]
username = rows[1]
password = rows[2]
errorCount = rows[3]
lock = rows[4]
#print("{} {} {} {} {}".format(no.value, username.value, password.value, errorCount.value, lock.value))
if _username == username.value:
#账户被锁定
if lock.value == True:
print("too many error count to input, the user {user} is locked,please contact administrator!".format(user=username.value))
break
#密码正确,此处需要把值统一为字符类型对比
elif _password == str(password.value):
print("welcome user {user} login !".format(user = username.value))
#将输入错误次数置0
userInfoSheet.cell(row=(no.value + 1), column=4).value = 0
userInfoFile.save("homeWork1CountInfo.xlsx")
break
else:
#密码错误,密码输入错误次数+1
errorCount.value = errorCount.value + 1
userInfoSheet.cell(row=(no.value+1), column=4).value = errorCount.value
userInfoFile.save("homeWork1CountInfo.xlsx")
print("invalid username or password")
#密码输入错误次数大于等于3,锁定账户
if errorCount.value > 2:
lock.value = True
userInfoSheet.cell(row=(no.value+1), column=5).value = True
userInfoFile.save("homeWork1CountInfo.xlsx")
break
else:
break