Week1作业: 登录小程序
编写登录接口
->输入用户名和密码
->认证成功后显示欢迎信息
->输错三次后锁定
import os
lock = False
fail_times = 0
uname = input("please input your name:")
passwd = input("input your password:")
#判断输入的用户是否锁定
if (os.path.exists(".\lock.txt")):
f = open(".\lock.txt", "r")
for line in f:
userlockedinfo = line.split(":")
if ((userlockedinfo[0] == uname) and ("locked" == userlockedinfo[1].strip())):
lock = True
f.close()
while (True):
#从文件获取用户的密码
if (os.path.exists("users.txt")):
f1 = open("users.txt", "r")
for line in f1:
userinfo = line.split(":")
if (userinfo[0] == uname):
password = userinfo[1]
user_exists_flag =True
break
if (user_exists_flag == False):
print("user isn't exists!")
exit(1)
#print(uname, userinfo[0], password)
else:
print("user data lost")
exit(2)
if (lock == True):
print("your account already locked.")
exit(3)
#比较输入密码是否正确,输入三次错误后锁定
if (passwd == password.strip()):
print("welcome!", uname)
fail_times = 0
break
elif(fail_times >= 3):
if (fail_times >= 3):
f2 = open(".\lock.txt", "w")
f2.write(uname +":locked\n")
f2.close()
exit(4)
else:
fail_times = fail_times + 1
uname = input("please try again, input your name:")
passwd = input("input your password:")
浙公网安备 33010602011771号